Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-03-31
2
Medium Priority
?
540 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 400 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

Independent Software Vendors: 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 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…
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…
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…
Suggested Courses

721 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