Solved

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

Posted on 2009-03-31
2
519 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Multiple Forms 4 48
vb6 connector to SQL Server 2 34
Modifying Conditional Format from VBA code 3 48
Advice in Xamarin 21 48
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now