ReadAllBytes from a bitmap in memory instead of a file

I can get the bytes of the image file as follows:

    Dim nBytes() As Byte
    nBytes = File.ReadAllBytes(uPath)

This works great, however I would like to get bytes from a bitmap that only exists in memory.

I would not want to save it to a file first.

Is this possible, and if so, how?

Thank.

+4
source share
1 answer

Assuming bitmap System.Drawing.Bitmap

Using ms As New MemoryStream()
    myBitmap.Save(ms, ImageFormat.xxx)
    ms.Position = 0

    Return ms.ToArray()
End Using

The string ms.Position = 0may not be needed when calling ToArray(). I set the memory stream to another property of the object stream, which should be reset in this situation.

+4
source

Source: https://habr.com/ru/post/1693382/


All Articles