locked
Byte to sql db column RRS feed

  • Question

  • User2098176964 posted

    I need to convert Byte to stream and save it to db field type image

    Protected Function Resize(ByVal strThumb As String) As Byte
            Dim objImage As System.Drawing.Image
            Dim intWidth As Integer
            Dim intHeight As Integer
            Dim TimesTwo As Double
            Dim x As Integer
            Dim y As Integer

            If IO.File.Exists(strThumb) Then
                objImage = Drawing.Image.FromFile(strThumb)

                intWidth = objImage.Width
                intHeight = objImage.Height

                If intHeight > intWidth Then ' portrait  
                    TimesTwo = 400 / intHeight
                Else
                    TimesTwo = 400 / intWidth
                End If

                x = CInt(intWidth * TimesTwo)
                y = CInt(intHeight * TimesTwo)


                Dim thumb As New Bitmap(objImage, x, y)

                Dim graphics As Graphics = graphics.FromImage(thumb)
                graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                graphics.SmoothingMode = SmoothingMode.HighQuality
                graphics.DrawImage(thumb, New Rectangle(0, 0, x, y), New Rectangle(0, 0, thumb.Width, thumb.Height), GraphicsUnit.Pixel)
                graphics.Dispose()

                'Write out the image in browser, so for so good
                Response.ContentType = "image/jpeg"
                thumb.Save(Response.OutputStream, ImageFormat.Jpeg)
                

               'Make the image av Stream and insert it to db
               Dim imgStream As New IO.MemoryStream
                thumb.Save(imgStream, ImageFormat.Jpeg)
                Dim c(imgStream.Length() - 1) As Byte
                imgStream.Read(c, 0, c.Length)
                imgStream.Close()

               How can I pass the value with stream to db

     

                'If I want to save it to file
                'thumb.Save(Server.MapPath("minFIl.jpg"), ImageFormat.Jpeg)
               

                thumb.Dispose()
                objImage.Dispose()
                IO.File.Delete(strThumb)
            End If
        End Function

    Sunday, November 30, 2008 8:51 AM

Answers

  • User2098176964 posted

     I've tried so many ways to insert an Image to an sql Image type field, and I need youre help

     

    This is the code that passes the image to the resize
      e.Values("AdImage") = Resize(Server.MapPath(imageAndPath))

    Error message:system.Drawing.Bitmap cannot convert to type System.Byte[]

     Protected Function Resize(ByVal strThumb As String) As Bitmap
            Dim objImage As System.Drawing.Image
            Dim intWidth As Integer
            Dim intHeight As Integer
            Dim TimesTwo As Double
            Dim x As Integer
            Dim y As Integer

            If IO.File.Exists(strThumb) Then
                objImage = Drawing.Image.FromFile(strThumb)

                intWidth = objImage.Width
                intHeight = objImage.Height

                If intHeight > intWidth Then ' portrait 
                    TimesTwo = 200 / intHeight
                Else
                    TimesTwo = 200 / intWidth
                End If

                x = CInt(intWidth * TimesTwo)
                y = CInt(intHeight * TimesTwo)


                Dim thumb As New Bitmap(objImage, x, y)

                Dim graphics As Graphics = graphics.FromImage(thumb)
                graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                graphics.SmoothingMode = SmoothingMode.HighQuality
                graphics.DrawImage(thumb, New Rectangle(0, 0, x, y), New Rectangle(0, 0, thumb.Width, thumb.Height), GraphicsUnit.Pixel)
                graphics.Dispose()

                Dim imgStream As New IO.MemoryStream
                thumb.Save(imgStream, ImageFormat.Jpeg)
                Dim c(imgStream.Length() - 1) As Byte
                imgStream.Read(c, 0, c.Length)
                imgStream.Close()

                Return thumb



                thumb.Dispose()
                objImage.Dispose()
                IO.File.Delete(strThumb)
            End If
        End Function

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 2, 2008 3:44 PM