Solved

Save bitmaps to SQL database and retrieve bitmaps from SQL database.

Posted on 2009-03-31
2
525 Views
Last Modified: 2013-12-26
I am saving bitmap to the SQL database as follows:

Adding input parameter to the stored procedure as follows:
dim MyPhoto  as system.Drawing.Bitmap
objCmd.Parameters("@imgInPhoto").Value = BitmapToByteArray(Me.MyPhoto)


Here is the utility function to convert bitmap to Byte array:
 Public Function BitmapToByteArray(ByRef SourceImage As System.Drawing.Bitmap) As Byte()
        If IsNothing(SourceImage) Then
            Dim objResult(0) As Byte
            Return objResult
        Else
            ' Local Variables.
            Dim objStream As New System.IO.MemoryStream()

            ' Get the data into a stream
            SourceImage.Save(objStream, System.Drawing.Imaging.ImageFormat.Bmp)
            objStream.Position = 0

            ' Now that we know the size, create our byte array.
            Dim objResult(Convert.ToInt32(objStream.Length)) As Byte

            ' Copy the data from the stream to the byte array.
            objStream.Read(objResult, 0, Convert.ToInt32(objStream.Length))

            ' return the results.
            Return objResult
        End If
    End Function

Saving Works fine.

Retrieving the Bitmap from database follows:

Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(150, 150)
imageListLarge.ColorDepth = ColorDepth.Depth32Bit
ListView1.View = View.LargeIcon
ListView1.LargeImageList = imageListLarge

 For Each i As DataRow In ds.Tables(0).Rows
            Dim objStream As New IO.MemoryStream(CType(i.Item("CrashPhoto"), Byte()))
            Static pb As New PictureBox
            pb.Size = imageListLarge.ImageSize
            pb.BackColor = ListView1.BackColor
            pb.SizeMode = PictureBoxSizeMode.Zoom

            j = j + 1
            pb.Image = New System.Drawing.Bitmap(objStream)
            Dim bmp As New Bitmap(pb.Width, pb.Height)
            pb.DrawToBitmap(bmp, pb.ClientRectangle)
            Dim key As String = j.ToString
            imageListLarge.Images.Add(j, bmp)
            ListView1.Items.Add("Pic #" & j, key)
        Next

My question is images are not showing properly, they are very blurred.
How to display images properly in the Listview.
0
Comment
Question by:Indyvasi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 24032965
Pretty sure you're doing an unnecessary array copy in your "save" routine:

    Public Function BitmapToByteArray(ByRef SourceImage As System.Drawing.Bitmap) As Byte()
        If IsNothing(SourceImage) Then
            Dim objResult(0) As Byte
            Return objResult
        Else
            ' Local Variables.
            Dim objStream As New System.IO.MemoryStream()
            SourceImage.Save(objStream, System.Drawing.Imaging.ImageFormat.Bmp)
            Return objStream.ToArray
        End If
    End Function

In your load method, try this line instead:

    pb.Image = Image.FromStream(objStream)

Note sure that will make much difference.
0
 

Author Closing Comment

by:Indyvasi
ID: 31565032
Worked.
Thank you very much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question