Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA to format 3 80
Window placement 17 65
Excel object stays open 19 65
Saving history changes to sub form 4 26
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

13 Experts available now in Live!

Get 1:1 Help Now