Answered by:
DrawImage Parameter Is Not Valid?!

Question
-
Okay, so here is the crux. I'm trying to manipulate an image a bit using Graphics.DrawImage. I've created a nice method to call for any new manipulation. The code looks as follows;
Image img = _SomeImage; Bitmap bmp = new Bitmap(this.Width + widthAddon, this.Height, PixelFormat.Format32bppArgb); PointF[] destPoints = { new PointF(changeableValueA, 0), new PointF(this.Width, 0), new PointF(changeableValueB, this.Height) }; Graphics graphics = Graphics.FromImage(bmp); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; graphics.DrawImage(img, destPoints); img.Dispose(); graphics.Dispose();
The code works fine, the first time, but whenever I make yet another call to this function I get "Parameter Is Not Valid" exception. So the first time works, the second makes it all crash... what to do, any clues?!Regards.
Wednesday, December 14, 2011 2:02 AM
Answers
-
Hi,
if _Someimage is a class-level/scope variable, this will fail after one successful run because you dispose the image at the end:
img.Dispose();
and img is pointing to the _someimage picture, so this will get disposed.
Either dont dispose img (and properly dispose _someimage when the form closes or so) or create a temporary copy
using (Image img = (Image)_SomeImage.Clone()) { //... your code here }
- Edited by Thorsten Gudera Wednesday, December 14, 2011 3:27 AM
- Proposed as answer by Ante Meridian Wednesday, December 14, 2011 8:12 AM
- Marked as answer by LuntarN Wednesday, December 14, 2011 2:43 PM
Wednesday, December 14, 2011 3:26 AM
All replies
-
Hi,
if _Someimage is a class-level/scope variable, this will fail after one successful run because you dispose the image at the end:
img.Dispose();
and img is pointing to the _someimage picture, so this will get disposed.
Either dont dispose img (and properly dispose _someimage when the form closes or so) or create a temporary copy
using (Image img = (Image)_SomeImage.Clone()) { //... your code here }
- Edited by Thorsten Gudera Wednesday, December 14, 2011 3:27 AM
- Proposed as answer by Ante Meridian Wednesday, December 14, 2011 8:12 AM
- Marked as answer by LuntarN Wednesday, December 14, 2011 2:43 PM
Wednesday, December 14, 2011 3:26 AM -
Hello Luntar
I hope this problem is coming due to graphics.Dispose();
so test this code
Image img = _SomeImage; Bitmap bmp = new Bitmap(this.Width + widthAddon, this.Height, PixelFormat.Format32bppArgb); PointF[] destPoints = { new PointF(changeableValueA, 0), new PointF(this.Width, 0), new PointF(changeableValueB, this.Height) }; Graphics graphics = Graphics.FromImage(bmp); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; graphics.DrawImage(img, destPoints); img.Dispose(); // graphics.Dispose(); bmp.Dispose();
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.Wednesday, December 14, 2011 5:44 AM