How to find reason for Generic GDI+ error when saving an image?

While I still did not find out the reason what exactly caused the error when saving the image, I found a workaround to apply: const string i1Path = @”c:\my\i1.jpg”; const string i2Path = @”c:\my\i2.jpg”; var i = Image.FromFile(i1Path); var i2 = new Bitmap(i); i2.Save(i2Path, ImageFormat.Jpeg); I.e. by copying the image internally into a Bitmap instance … Read more

Translate Rectangle Position in Zoom Mode Picturebox

You can translate selected rectangle on the picture box to the rectangle on image this way: public RectangleF GetRectangeOnImage(PictureBox p, Rectangle selectionRect) { var method = typeof(PictureBox).GetMethod(“ImageRectangleFromSizeMode”, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var imageRect = (Rectangle)method.Invoke(p, new object[] { p.SizeMode }); if (p.Image == null) return selectionRect; var cx = (float)p.Image.Width / (float)imageRect.Width; var cy = (float)p.Image.Height … Read more

Alternatives to System.Drawing for use with ASP.NET?

There is an excellent blog post including C# code about using the ImageMagick graphics library through Interop over at TopTen Software Blog. This post deals specifically with running ASP.net on linux under mono; however, the C# code should be perfectly copy-paste-able, the only thing you’ll need to change is the Interop attributes if you are … Read more

c# Image resizing to different size while preserving aspect ratio

This should do it. private void resizeImage(string path, string originalFilename, /* note changed names */ int canvasWidth, int canvasHeight, /* new */ int originalWidth, int originalHeight) { Image image = Image.FromFile(path + originalFilename); System.Drawing.Image thumbnail = new Bitmap(canvasWidth, canvasHeight); // changed parm names System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(thumbnail); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.PixelOffsetMode = … Read more