usually, we only save text strings into tables but how do we save a real image ( ex: img.jpg ) into a table? regards,
you can save images in binary format in to MSSQL table field. First you have to convert the image binary and save it to the table. in MSSQL there is a data type called image for this. Hope this helps.
That's completely true but if you want to save images in the database like postcard, you save address of the image like a text be saved in a field and use it
What you are looking for is called "blob". search something like "blob sql images" However, i would recommend keeping it in the DB as a string of the image path, and not the blob Good luck
Here is some code that I used to upload image with the fileupload control into an Access database. I hope you can use it. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myFile As HttpPostedFile = FileUpload1.PostedFile Dim nFileLen As Integer = myFile.ContentLength Dim myData(nFileLen) As Byte myFile.InputStream.Read(myData, 0, nFileLen) Dim ms As IO.MemoryStream = New IO.MemoryStream(myData) Dim img As Drawing.Image = convertImg(ms) img = resizeImage(img, width.Text.Trim & "x" & height.Text.Trim) Dim bytes() As Byte = imgToBytes(img) Dim ret As String = wService.storeImage(title.Text, description.Text, location.Text, tags.Text, width.Text, height.Text, Date.Now, bytes) Literal1.Text = "http://www.sintmaartenlive.com/imgGrabing.aspx?id=" & ret & "<br/>" & bytes.Length & " Bytes Uploaded<br/><img src=""imgGrabing.aspx?id=" & ret & """>" wService.Dispose() ms.Dispose() img.Dispose() End Sub Public Function saveImg(ByVal iTitle As String, ByVal idescription As String, ByVal ilocation As String, ByVal iTags As String, ByVal imgwidth As Integer, ByVal imgheight As Integer, ByVal idate As Date, ByVal picImg As Byte()) As string Try Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Stillimages.mdb" Dim oleDBConn As New Data.OleDb.OleDbConnection(strConn) oleDBConn.Open() Dim oleDBAdapter As Data.OleDb.OleDbDataAdapter = New Data.OleDb.OleDbDataAdapter("Select * From Pictures", strConn) Dim oleDBcb As Data.OleDb.OleDbCommandBuilder = New Data.OleDb.OleDbCommandBuilder(oleDBAdapter) Dim ds As Data.DataSet = New Data.DataSet oleDBAdapter.MissingSchemaAction = Data.MissingSchemaAction.AddWithKey oleDBAdapter.Fill(ds, "Pictures") Dim dsRow As Data.DataRow = ds.Tables(0).NewRow dsRow.BeginEdit() dsRow("Title") = iTitle dsRow("Description") = idescription dsRow("imgWidth") = imgwidth dsRow("imgHeight") = imgheight dsRow("DateTaken") = idate dsRow("Location") = ilocation dsRow("Tags") = iTags dsRow("PicImg") = picImg dsRow.EndEdit() ds.Tables(0).Rows.Add(dsRow) oleDBAdapter.Update(ds.Tables(0)) ds.AcceptChanges() dim test as String = dsrow("id").tostring oleDBConn.Dispose() oleDBcb.Dispose() oleDBAdapter.Dispose() return test Return True Catch ex As Exception Return False End Try End Function Public Function imgToBytes(ByVal bmp As Drawing.Image) As Byte() Dim ms As New IO.MemoryStream bmp.Save(ms, Drawing.Imaging.ImageFormat.Jpeg) Dim abyt(ms.Length - 1) As Byte ms.Seek(0, IO.SeekOrigin.Begin) ms.Read(abyt, 0, ms.Length) Return abyt End Function I nowadays save it in MySQL db.
Nice code. How do you display it safely on an asp page. I've used some software in the past from webwiz that did a similar thing, but a hacker loaded 'something' onto my database that when i displayed it caused the site to time out. Is this a common problem and is the solution in how i display it or in somehow improving checks on whats being loaded? thanks
To display it on the webpage you do the following: Create a page called e.g. getimg.aspx copy this code into it: <%@ Page Language="VB" %> <script runat="server"> Public Sub grabImg(ByVal cameraid As Integer, ByVal size As String) Dim icf() As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders Dim encps As Drawing.Imaging.EncoderParameters = New Drawing.Imaging.EncoderParameters(1) encps.Param(0) = New Drawing.Imaging.EncoderParameter(Drawing.Imaging.Encoder.Quality, 100) Dim mdbConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/stillimages.mdb;Persist Security Info=False") Dim mdbCmd As New System.Data.OleDb.OleDbCommand("SELECT picImg FROM Pictures WHERE id = " & cameraid, mdbConn) mdbConn.Open() Dim mdbReader As System.Data.OleDb.OleDbDataReader = mdbCmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess) mdbReader.Read() Dim bits() As Byte = mdbReader.Item(0) Dim sys As System.IO.MemoryStream = New System.IO.MemoryStream(bits) mdbReader.Close() mdbConn.Close() Dim oImg As System.Drawing.Image = System.Drawing.Image.FromStream(sys) If size.Contains("x") Then oImg = resizeImage(oImg, size) oImg = watermarkIt(oImg) Response.Buffer = True Response.ContentType = "Image/JPEG" oImg.Save(Response.OutputStream, icf(1), encps) oImg.Dispose() End Sub Public Function watermarkIt(ByVal bmp As Drawing.Image) As Drawing.Image Dim strWatermark As String = "SintMaartenLive.com" Dim canvas As Drawing.Graphics = Drawing.Graphics.FromImage(bmp) Dim wmFont As Drawing.Font = New Drawing.Font("Verdana", 14, Drawing.FontStyle.Bold) Dim DesiredWidth As Single = bmp.Width * 0.75 Dim StringSizeF As Drawing.SizeF = canvas.MeasureString(strWatermark, wmFont) Dim Ratio As Single = StringSizeF.Width / wmFont.SizeInPoints Dim RequiredFontSize As Single = DesiredWidth / Ratio wmFont = New Drawing.Font("Verdana", RequiredFontSize, Drawing.FontStyle.Bold) canvas.DrawString(strWatermark, wmFont, New Drawing.SolidBrush(Drawing.Color.FromArgb(125, 0, 0, 0)), 3, 3) canvas.DrawString(strWatermark, wmFont, New Drawing.SolidBrush(Drawing.Color.FromArgb(150, 255, 255, 255)), 0, 0) Return bmp End Function Public Function resizeImage(ByVal img As Drawing.Image, ByVal size As String) as drawing.image Dim oImg As Drawing.Image = img Dim width As Integer = size.Split("x")(0) Dim height As Integer = size.Split("x")(1) Dim oThumbNail As Drawing.Image = New Drawing.Bitmap(width, height, oImg.PixelFormat) Dim oGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(oThumbNail) oGraphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality oGraphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality oGraphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic Dim oRectangle As Drawing.Rectangle = New Drawing.Rectangle(0, 0, oThumbNail.Width, oThumbNail.Height) oGraphic.DrawImage(oImg, oRectangle) oImg.Dispose() Return oThumbNail End Function Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim cameraid As String = Request.QueryString("id") Dim xsize As String = Request.QueryString("size") If String.IsNullOrEmpty(xsize) Then cameraid = 1 xsize = "139x95" End If grabImg(cameraid, xsize) Catch ex As Exception End Try End Sub </script> On your page you want to show the image you reference to the id for the image the database and just use a query string. Just like this: <img scr="getimg.aspx?id=1&size=200x100" /> As you can see you can even give a size and as you might notice it also watermark the image for you.
i recommend you to save the image in a directory, and put the "url information (path)" into the database.