Solved

Image to memory stream

Posted on 2006-11-28
7
260 Views
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?
0
Comment
Question by:JPERKS1985
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18033988
Hi JPERKS1985,

try something like this (from: http://www.dotnet247.com/247reference/msgs/22/112124.aspx):

        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)

Cheers,
Hillwaaa
0
 
LVL 1

Author Comment

by:JPERKS1985
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

            Try

                hash = md5.ComputeHash(Fs)

                Fs.Close()
                md5.Clear()

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

                Return Nothing
            End Try

0
 
LVL 16

Accepted Solution

by:
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:JPERKS1985
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
                Try
                    Do
                        ReDim Preserve ayRet(icnt)
                        '* Read the Byte into the array
                        ayRet(icnt) = poStream.ReadByte
                        '* Increment
                        icnt = (icnt + 1)
                    Loop
                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.
0
 
LVL 16

Expert Comment

by:Hillwaaa
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)
Messagebox.show(hash)

0
 
LVL 1

Author Comment

by:JPERKS1985
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
0
 
LVL 16

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
passing a value with stream reader AFTER a ";" 3 42
DataGridView 2 29
regex to allow alphanumeric characters and \ 2 26
Expando 4 36
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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