c# - Resize image gdi+ graphics .net -


I have this method of shrinking an image for that website on which I am working:

  Static Byte [] Create Short Routes (blogfoto blogfoto, int newmaxwidth, int newmaxhit) {Image img; (Memorystream original image = use new memorystream (blogfoto blogfoto image)) {img = Image.FromStream (original image); } Int Nudith; Int newHeight; Byte [arr] If (img.Width> img.Height) {if (img.Width & lt; = newMaxWidth) {(memory stream thumbStr = new memorystream ()) {img.Save (thumbStr, ImageFormat.Jpeg); Img.Dispose (); Arr = thumbStr.ToArray (); } Return arr; } NewWidth = newMaxWidth; NewHeight = (int) ((Float) New Wideth / (Float) IMG Width) * (Float) IMG Highlight); } Else {if (img.Height & lt; = newMaxHeight) {(memory stream thumbStr = new memorystream ()) {img.Save (thumbStr, ImageFormat.Jpeg); Img.Dispose (); Arr = thumbStr.ToArray (); } Return arr; } NewHeight = newMaxHeight; New Wide = (int) ((Float) NewHigh / (Float) IMG Highlight) * (Float) IMG with); } Image thumb = new bitmap (newdefender, newhigh); Graphics G = Graphics Frame image (thumbs); G. Interpolation mode = Interpolation mode. Highquite bibbic; G. SabootingMode = Smoothing. High quality; G.PixelOffsetMode = PixelOffsetMode.HighQuality; G.CompositingQuality = CompositingQuality.HighQuality; G.DrawImage (img, 0f, 0f, (float) new watt, (float newheit); (Using MemoryStream thumbStr = new memorystream ()) {thumb.Save (thumbStr, ImageFormat.Jpeg); Arr = thumbStr.ToArray (); } G.Dispose (); Img.Dispose (); Return arr; } Most of the time it works great but sometimes it gives me the exception: a common error occurred in GDI + error code -2147467259 Source: "System. Drawing" This Image .Save is on (... I tried to make this code as possibly defensive, but still it is not happening what is happening. If someone can know a good answer, then the critics also welcome . 

Document the image See FM Stream ()

You need to open the stream for the lifetime of the image. First open the memory stream now, and it should work.


Comments