question

RogerSchlueter-7899 avatar image
0 Votes"
RogerSchlueter-7899 asked PeterFleischer-3316 answered

Why this error when doing an image conversion

I am trying to convert a BitmapImage to a Byte() with the following code:

 Dim ic As New ImageConverter
 Dim bts() As Byte = DirectCast(ic.ConvertTo(clsImage, GetType(Byte())), Byte())

The error message I get is:

> Unable to cast object of type 'System.Windows.Media.Imaging.BitmapImage' to type 'System.Drawing.Image'.

The code does NOT attempt to convert the BitmapImage to another image so I don't how to fix this problem.



windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

Hi, System.Drawing.ImageConverter converts objects from System.Drawing Namespace. Objects from namespace System.Windows.Media (PresentationCore.dll) like BitmapImage must be converted explicitly. Get byte array you can use this code:

 Dim ms = TryCast(clsImage.StreamSource, MemoryStream)
 Dim bts As Byte() = ms.ToArray
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.