1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

want to put images into a DB table.

Discussion in 'C#' started by wacamoi, Oct 23, 2008.

  1. #1
    usually, we only save text strings into tables

    but how do we save a real image ( ex: img.jpg ) into a table?


    regards,
     
    wacamoi, Oct 23, 2008 IP
  2. sampathsl

    sampathsl Guest

    Messages:
    861
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    sampathsl, Oct 25, 2008 IP
  3. palme

    palme Active Member

    Messages:
    320
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    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
     
    palme, Oct 25, 2008 IP
  4. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    ranabra, Oct 26, 2008 IP
  5. Ehmad

    Ehmad Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ehmad, Oct 27, 2008 IP
  6. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #6
    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.
     
    islandhopper8, Nov 1, 2008 IP
    wacamoi likes this.
  7. bodyvision

    bodyvision Member

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    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
     
    bodyvision, Nov 13, 2008 IP
  8. basdotnet

    basdotnet Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    basdotnet, Nov 13, 2008 IP
  9. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #9
    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.
     
    islandhopper8, Dec 17, 2008 IP
  10. jhannell

    jhannell Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    i recommend you to save the image in a directory, and put the "url information (path)" into the database.
     
    jhannell, Jan 2, 2009 IP
  11. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #11
    Sure you can do that too. However personally I like to store it in database. Works easier for me.
     
    islandhopper8, Feb 24, 2009 IP
  12. wacamoi

    wacamoi Peon

    Messages:
    810
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #12
    basic I want to save images into DB and these images can only be read by password.
     
    wacamoi, Feb 24, 2009 IP
  13. ilarathka

    ilarathka Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If you are using Oracle 9i and above you will get data type for image storing
     
    ilarathka, Mar 5, 2009 IP
    wacamoi likes this.