none
Graphics to varbinary(max)

    Frage

  • Hi

    How can I save a Graphics type value to a varbinary(max) field? Do I need to do some conversion or can the Graphics value be directly assigned to the varbinary(max) field?

    Thanks

    Regards

    Freitag, 17. Februar 2012 18:25

Antworten

  • Hi

    Below code is what I tried with help from several sources and it works. It nicely resizes the picture from file system source and saves into a varbinary(max) field. I have also included the resize function.

    Hope this helps someone.

    Regards

    SUB FillPictures()
      ' ...
      Dim Path As String = ""
      Dim j As Integer = 0
      Do While (j < MyDataTable.Rows.Count)
        Path = MyDataTable.Rows(j)("PhotoPath").ToString
        Using picture As Image = Image.FromFile(Path)
          Using stream As New IO.MemoryStream
             ResizeImage(picture, 100, 100).Save(stream, Imaging.ImageFormat.Jpeg)
             MyDatable.Rows(j)("Photo") = stream.GetBuffer()
          End Using
        End Using
      Loop
      MyAdapter.Fill(MyDataTable)
    End Sub
    SUB ResizeImage(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
            Dim sourceWidth As Integer = imgPhoto.Width
            Dim sourceHeight As Integer = imgPhoto.Height
            Dim sourceX As Integer = 0
            Dim sourceY As Integer = 0
            Dim destX As Integer = 0
            Dim destY As Integer = 0
            Dim nPercent As Single = 0
            Dim nPercentW As Single = 0
            Dim nPercentH As Single = 0
            nPercentW = (CType(Width, Single) / CType(sourceWidth, Single))
            nPercentH = (CType(Height, Single) / CType(sourceHeight, Single))
            If (nPercentH < nPercentW) Then
                nPercent = nPercentH
                destX = System.Convert.ToInt16(((Width _
                                - (sourceWidth * nPercent)) _
                                / 2))
            Else
                nPercent = nPercentW
                destY = System.Convert.ToInt16(((Height _
                                - (sourceHeight * nPercent)) _
                                / 2))
            End If
            Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
            Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
            Dim bmPhoto As Bitmap = New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
            Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
            grPhoto.Clear(Color.White)
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
            grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)
            grPhoto.Dispose()
            Return bmPhoto
        End Function

    Freitag, 2. März 2012 09:27

Alle Antworten

  • Hi,

    You may take a look on this article: http://www.sqlmag.com/article/tsql3/varbinary-max-tames-the-blob 

    I hope it helps.

    J.


    There are 10 type of people. Those who understand binary and those who do not.
    My Blog

    Samstag, 18. Februar 2012 19:32
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Freitag, 2. März 2012 08:55
    Moderator
  • Hi

    Below code is what I tried with help from several sources and it works. It nicely resizes the picture from file system source and saves into a varbinary(max) field. I have also included the resize function.

    Hope this helps someone.

    Regards

    SUB FillPictures()
      ' ...
      Dim Path As String = ""
      Dim j As Integer = 0
      Do While (j < MyDataTable.Rows.Count)
        Path = MyDataTable.Rows(j)("PhotoPath").ToString
        Using picture As Image = Image.FromFile(Path)
          Using stream As New IO.MemoryStream
             ResizeImage(picture, 100, 100).Save(stream, Imaging.ImageFormat.Jpeg)
             MyDatable.Rows(j)("Photo") = stream.GetBuffer()
          End Using
        End Using
      Loop
      MyAdapter.Fill(MyDataTable)
    End Sub
    SUB ResizeImage(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
            Dim sourceWidth As Integer = imgPhoto.Width
            Dim sourceHeight As Integer = imgPhoto.Height
            Dim sourceX As Integer = 0
            Dim sourceY As Integer = 0
            Dim destX As Integer = 0
            Dim destY As Integer = 0
            Dim nPercent As Single = 0
            Dim nPercentW As Single = 0
            Dim nPercentH As Single = 0
            nPercentW = (CType(Width, Single) / CType(sourceWidth, Single))
            nPercentH = (CType(Height, Single) / CType(sourceHeight, Single))
            If (nPercentH < nPercentW) Then
                nPercent = nPercentH
                destX = System.Convert.ToInt16(((Width _
                                - (sourceWidth * nPercent)) _
                                / 2))
            Else
                nPercent = nPercentW
                destY = System.Convert.ToInt16(((Height _
                                - (sourceHeight * nPercent)) _
                                / 2))
            End If
            Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
            Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
            Dim bmPhoto As Bitmap = New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
            Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
            grPhoto.Clear(Color.White)
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
            grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)
            grPhoto.Dispose()
            Return bmPhoto
        End Function

    Freitag, 2. März 2012 09:27