0

I am trying to convert

System.Drawing.Image from Stream

but its throwing Parameter not valid exception.

 Stream p_sImageStream = GenerateStreamFromString(p_sImage);
 System.Drawing.Image oIM = Bitmap.FromStream(p_sImageStream);

my p_sImage is "56427673422d0cbd5dfdfebc_M-19__wide-1.JPG"

MethodMan
  • 18,625
  • 6
  • 34
  • 52
XXDebugger
  • 1,581
  • 3
  • 26
  • 48
  • A drawing is a binary file and you should never convert it to a string. Binary data should always use Encoding UTF8 and be converted to a byte[] instead of a string. Most string classes default to Encoding Ascii which will corrupt binary data. So make sure you specify the encoding when using binary data. – jdweng Nov 13 '15 at 17:27
  • thats y i m converting it to a stream first and then drawing. where am I going wrong. – XXDebugger Nov 13 '15 at 17:28
  • show code for `GenerateStreamFromString()` – jsanalytics Nov 13 '15 at 17:30
  • That is not a "stream", that is a filename. Use Image.FromFile() instead. – Hans Passant Nov 13 '15 at 17:31
  • @jstreet: Probably gotten from http://stackoverflow.com/q/1879395/103167 – Ben Voigt Nov 13 '15 at 17:33

2 Answers2

2

The parameter to GenerateStreamFromString is not a filename, it's expected to contain the actual data.

If you have the filename of a JPEG file, use File.OpenRead to get a stream you can pass to Bitmap.FromStream, or else just use new Bitmap(filename) -- this constructor overload opens and reads a file.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Try this

string p_sImage = @"c:\temp\56427673422d0cbd5dfdfebc_M-19__wide-1.JPG";
FileStream stream = new FileStream(p_sImage, FileMode.Open);
System.Drawing.Image oIM = Bitmap.FromStream(stream);
yanckst
  • 438
  • 4
  • 17
jdweng
  • 33,250
  • 2
  • 15
  • 20