Hi.
My name is Akmal. And I try to use C# for my job. I need to draw some images on PictureBox control. This is code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace test_Move_Image { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Image map; Bitmap bmp; private void button1_Click(object sender, EventArgs e) { Image img = Image.FromFile(@"c:\MAP_TAS\wash.jpg"); Bitmap tbmp = new Bitmap(img.Width, img.Height); Graphics tgr = Graphics.FromImage(tbmp); tgr.DrawImage(img, 0, 0); map = tbmp; draw_map(); tgr.Dispose(); tbmp.Dispose(); } private void draw_map() { pictureBox1.Image = null; pictureBox1.Image = map; GC.Collect(); GC.WaitForPendingFinalizers(); }
} }
|
Try to run this code and you will see the erorr. But i don't uderstand where the problem in my code?
Please help me.
Suthish NairPosted Feb 16, 2011, 2:48 AM
Akmal AliPosted Feb 15, 2011, 1:48 AM
Thank's a lot Frogleg. I have tried your code and the problem has disappeared.
Thank's to all for your assistance and for your attention.
Sam HobbsPosted Feb 15, 2011, 1:33 AM
FroglegPosted Feb 15, 2011, 1:25 AM
instead of
map = tbmp;
use
Rectangle rct = new Rectangle(0,0,tbmp.Width,tbmp.Height);
map = tbmp.Clone(rct, tbmp.PixelFormat);
if map is equal to tbmp and tbmp is disposed - map = null
Sam HobbsPosted Feb 15, 2011, 1:14 AM
You did not say if you understand about Paint. I think the answer to the question of why you are getting the error is that Windows needs the object to do the Paint. If you don't understand about painting in Windows then you need to learn about that.
Here is a simple rule. Don't call Dispose unless you know you need it.
Akmal AliPosted Feb 15, 2011, 12:47 AM
Sam HobbsPosted Feb 15, 2011, 12:36 AM
When I remove (comment out) the lines that call tgr.Dispose() and tbmp.Dispose() then the error is gone. I am not experienced with graphics in .Net but I really doubt that you need to call Dispose for those two objects; it is probably just a problem to call Dispose for them.
Akmal AliPosted Feb 14, 2011, 11:57 PM
Below I will show the error message and a screen shot.
/*
System.ArgumentException ?? ??????????
Message="???????????? ????????."
Source="System.Drawing"
StackTrace:
? System.Drawing.Image.get_Width()
? System.Drawing.Image.get_Size()
? System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
? System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
? System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
? System.Windows.Forms.Control.WmPaint(Message& m)
? System.Windows.Forms.Control.WndProc(Message& m)
? System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
? System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
? System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
? System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
? System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
? System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
? System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
? System.Windows.Forms.Application.Run(Form mainForm)
? test_Move_Image.Program.Main() ? C:\Documents and Settings\?????????????\??? ?????????\Visual Studio 2008\Projects\test_Move_Image\test_Move_Image\Program.cs:?????? 18
? System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
? System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
? Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
? System.Threading.ThreadHelper.ThreadStart_Context(Object state)
? System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
? System.Threading.ThreadHelper.ThreadStart()
InnerException:
*/
Sam HobbsPosted Feb 14, 2011, 6:14 PM
Suthish NairPosted Feb 14, 2011, 10:10 AM
Prasant JinagaPosted Feb 14, 2011, 6:55 AM
Hi Akmal,
Put the image in solution explorer and try..Hope it will work.
Thanks,
Prasant