I need images saved by an old vb6 program and convert them to a new db of a new prog. The old programma saved the images data into a image field of a SQL2000 DB. First I justed copy the image from V1 (db of the old programma) to V2 (the db (also SQL2000) used in the new program).
Problem: the data in the image field is corrupt! When I try to convert that data into a bitmap or jpg it gives me a Invalid parameter Exception when I do this code:
MemoryStream ImageDataStream = new MemoryStream();
// byte[] ImageData, filled with the image field of db
ImageDataStream.Write(ImageData,0,ImageData.Length);
ImageDataStream.Position=0;
Image img = Image.FromStream(ImageDataStream); //Invalid parameter Exception thrown
This is because the byte data of ImageData is corrupt due a Unicode encoding done by the old VB6 programma before/while saving it to db. Below some byte code (hex style) so that you can see the the good data vs the corrupt/wrong data:
A bitmap header starts with BM, so there you see that the is a doubling of the data: from 42 4D to 42 00 4D 00 => convertion to Unicode. You can't simply remove the 00 because unicoding is not just a 00 extend. For example 75 87 95 59 (see above in picture) will be converted to 75 00 21 20 22 20 59 00 and not just 75 00 87 00 95 00 59 00 !!
So what you have to do is convert the data back to the current ANSI code page (Encoding.Default):
ImageData=System.Text.UnicodeEncoding.Convert(Encoding.Unicode,Encoding.Default,ImageData);
So the code below will do the good convertion......
MemoryStream ImageDataStream = new MemoryStream();
ImageDataStream.Write(ImageData,0,ImageData.Length);
ImageDataStream.Position=0;
ImageData=System.Text.UnicodeEncoding.Convert(Encoding.Unicode,Encoding.Default,ImageData);
Image img = Image.FromStream(ImageDataStream);
Not every "Invalid parameter used" exception will be fixed like that, sometimes you need an other Convertion with other Encoder parameters or the image can have a added header (like the 78 long ole-header,..... => the code will be like ImageDataStream.Write(ImageData,78,ImageData.Length-78); )
Good luck!
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten