PrivateSub btnUpload_Click()Sub btnUpload_Click()Sub btnUpload_Click()Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click Dim intImageSize As Int32 Dim strImageType AsString Dim imageStream As Stream intImageSize = myImageFile.PostedFile.ContentLength strImageType = myImageFile.PostedFile.ContentType imageStream = myImageFile.PostedFile.InputStream If intImageSize <=0Then Response.Write("Picture can't be null") Return EndIf Dim imageContent(intImageSize) AsByte Dim intStatus As Int32 intStatus = imageStream.Read(imageContent, 0, intImageSize) Dim dbAccess AsNew CommonDB Try dbAccess.OpenConnection() dbAccess.Command.CommandText ="INSERT INTO imgProcess(pic_title,pic_type,pic_content) VALUES (@imgtitle,@imgtype,@imgdata )" dbAccess.Command.Parameters.Add("@imgtitle", SqlDbType.VarChar, 50).Value = txtTitle.Text.Trim dbAccess.Command.Parameters.Add("@imgtype", SqlDbType.VarChar, 50).Value = strImageType dbAccess.Command.Parameters.Add("@imgdata", SqlDbType.Image).Value = imageContent dbAccess.Command.ExecuteNonQuery() Response.Write("Success!") Catch ex As Exception Response.Write(dbAccess.ErrorMessage) Finally dbAccess.CloseConnection() EndTry End Sub
PrivateSub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load IfNot IsPostBack Then ReBind() EndIf End Sub PrivateSub ReBind()Sub ReBind() Dim dbAccess AsNew CommonDB Try dbAccess.OpenConnection() dbAccess.Command.CommandText ="SELECT pic_id,pic_title FROM imgProcess " Grid1.DataSource = dbAccess.Command.ExecuteReader Grid1.DataBind() Catch ex As Exception Response.Write(ex.ToString) Finally dbAccess.CloseConnection() EndTry End Sub
Imports System Imports System.Web Imports System.Data Imports System.Data.SqlClient Imports System.Drawing Imports System.Drawing.Imaging Imports System.IO PublicClass GetImgClass GetImg : Implements IHttpHandler PublicSub ProcessRequest()Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim strId AsString= Convert.ToString(context.Request.Item("id")) If strId IsNothingThen Return EndIf Dim stream AsNew MemoryStream Dim bm As Bitmap Dim img As Image Dim dbAccess AsNew CommonDB Try dbAccess.OpenConnection() dbAccess.Command.CommandText ="SELECT pic_content FROM imgProcess WHERE pic_id="& strId Dim blob() AsByte= dbAccess.Command.ExecuteScalar() stream.Write(blob, 0, blob.Length) bm =New Bitmap(stream) Dim width As Int16 =200 Dim height As Int16 = bm.Height / bm.Height * width img = bm.GetThumbnailImage(width, height, Nothing, IntPtr.Zero) img.Save(context.Response.OutputStream, ImageFormat.Jpeg) Catch ex As Exception context.Response.Write(ex.ToString) Finally dbAccess.CloseConnection() IfNot (img IsNothing) Then img.Dispose() EndIf IfNot (bm IsNothing) Then bm.Dispose() EndIf stream.Close() EndTry End Sub PublicReadOnlyProperty IsReuseable()Property IsReuseable() AsBooleanImplements IHttpHandler.IsReusable Get ReturnTrue EndGet End Property End Class
PrivateSub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load Dim dbAccess AsNew CommonDB Try dbAccess.OpenConnection() dbAccess.Command.CommandText ="SELECT * FROM imgProcess WHERE pic_id = "& Request.QueryString("id").Trim dbAccess.Reader = dbAccess.Command.ExecuteReader() DoWhile (dbAccess.Reader.Read()) Response.ContentType = dbAccess.Reader.Item("pic_type") Response.BinaryWrite(dbAccess.Reader.Item("pic_content")) Loop Catch ex As Exception Response.Write(dbAccess.ErrorMessage) Finally dbAccess.CloseConnection() EndTry End Sub