MemoryStream() method show Exception

N

Neeraj

hi
i am working on dicom image.this is code which gives exception on
"System.Drawing.Image imgInFile = System.Drawing.Image.FromStream(new
System.IO.MemoryStream(destPixels ) "
..The exception is "System.ArgumentException: Parameter is not valid."
could any body help me
i am waiting for help
regards,
Neeraj Kumar



protected internal virtual System.Drawing.Image scaleImage()
{
int scaledWidth = w / 2;
int scaledHeight = h / 2;
int index = 0;
int value_Renamed = 0;
byte[] destPixels = null;
this.pixData = dHR.data;
System.GC.Collect();

destPixels = new byte[scaledWidth * scaledHeight];
for (int i = 0; i < h; i += 2)
{
for (int j = 0; j < w; j += 2)
{
destPixels[index++] = (byte)pixData[(i * w) + j];
}
}
pixData = null;
Tools.gc("PIXDATA == NULL");
System.Drawing.Image imgInFile =
System.Drawing.Image.FromStream(new System.IO.MemoryStream(destPixels )
return imgInFile;
}
 
J

Jon Skeet [C# MVP]

Neeraj said:
i am working on dicom image.this is code which gives exception on
"System.Drawing.Image imgInFile = System.Drawing.Image.FromStream(new
System.IO.MemoryStream(destPixels ) "
.The exception is "System.ArgumentException: Parameter is not valid."

Indeed. Image.FromStream expects you to provide an image file in one of
the supported formats - jpeg, gif etc. You've given raw data.

(It's unfortunate that the documentation doesn't make this obvious,
admittedly.)
 
G

Greg Young

Some code I have had laying around here for a while; This should help your
problem as well.

public System.Drawing.Image ResizeImage(System.Drawing.Image
poImage, System.Drawing.Size poSize) {
//Detach image from its source
System.Drawing.Image oImageOriginal =
(System.Drawing.Image)poImage.Clone();

//Resize new image
System.Drawing.Image oResizedImage = new
System.Drawing.Bitmap(poSize.Width, poSize.Height,
oImageOriginal.PixelFormat);
System.Drawing.Graphics oGraphic =
Graphics.FromImage(oResizedImage);
oGraphic.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality ;
oGraphic.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
oGraphic.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic ;
Rectangle oRectangle = new Rectangle(0, 0,
poSize.Width, poSize.Height);
oGraphic.DrawImage(oImageOriginal, oRectangle);
oGraphic.Dispose() ;
oImageOriginal.Dispose();
return oResizedImage;
}

Cheers,

Greg Young
MVP - C#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top