Image to memory stream

Posted on 2006-11-28
Last Modified: 2010-04-23
I have a function i'm using to generate an md5 off an image

 Dim MD5ImageStream As New IO.MemoryStream(ayfile)

ayfile represents "capacity as integer"

I was using Dim ayFile() As Byte = ConnectStreamToByteArray(oStream)
before. Now I'm using

   Dim oImage As Image = Image.FromStream(oStream)

What would I pass to md5imagestream in place of ayfile?
Question by:JPERKS1985
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
  • 4
  • 3
LVL 16

Expert Comment

ID: 18033988
Hi JPERKS1985,

try something like this (from:

        Dim myImage As Image
        Dim ms As New IO.MemoryStream
        myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim b(ms.Length - 1) As Byte
        ms.Position = 0
        ms.Read(b, 0, ms.Length)


Author Comment

ID: 18034062
That doesn't seem to work. Thanks for your help thus far. The stream is created this way

  Dim oStream As Stream = oResponse.GetResponseStream

I must be able to pass the finished product to

  Private Function MD5_Hash(ByVal Fs As Stream) As String
            Dim md5 As New MD5CryptoServiceProvider
            Dim hash() As Byte


                hash = md5.ComputeHash(Fs)


                Return BitConverter.ToString(hash).Replace("-", "").ToUpper
            Catch ex As Exception

                Return Nothing
            End Try

LVL 16

Accepted Solution

Hillwaaa earned 500 total points
ID: 18034096
Ok, not sure if I'm confused now, but i thought you wanted a replacement for ayfile, so something like this:

Dim oImage As Image = Image.FromStream(oStream)
Dim MD5ImageStream As New IO.MemoryStream
oImage.Save(MD5ImageStream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim hash As String = MD5_Hash(MD5ImageStream)

Is that what you wanted?
Technology Partners: 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!


Author Comment

ID: 18034121
sorry I'm starying to confuse myself,

I used to use

        Private Function ConnectStreamToByteArray(ByVal poStream As Stream) As Byte()

            Dim obj45 As Object = New Object

            SyncLock obj45

                Dim ayRet() As Byte
                Dim icnt As Integer
                        ReDim Preserve ayRet(icnt)
                        '* Read the Byte into the array
                        ayRet(icnt) = poStream.ReadByte
                        '* Increment
                        icnt = (icnt + 1)
                Catch ex As Exception
                    '* Do nothing. The Stream is Completed
                End Try
                '* Return the Byte Array

                Return ayRet

            End SyncLock

        End Function

To convert ostream into the proper format, however that was slowing down the thread. Now I need an alternative to either use ostream to convert to the proper formatfrom either oimage or ostream or even a modficatio to the hash function so I can pass the oimage directly.
LVL 16

Expert Comment

ID: 18034161
What happens when you try the following (I'm assuming that you already have oImage as an Image - and you might need to change the image format to the appropriate format):

Dim MD5ImageStream As New IO.MemoryStream
oImage.Save(MD5ImageStream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim hash As String = MD5_Hash(MD5ImageStream)


Author Comment

ID: 18034190
You got it man , I get a good hash (thats not the default blank hash). Thanks for the help id buy you a drink if we wern't online. haha
LVL 16

Expert Comment

ID: 18034201
lol - no worries!  buy it anyway and drink it for me :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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