Solved

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

Posted on 2009-03-31
2
520 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
2 Comments
 
LVL 85

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now