I would like to be able to move the image inside the PictureBox. And I found solution, but it works very slow. Why? May need to use another method? Prompt me please.
Akamal.
11 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 15, 2011, 3:34 AM EST
0
My recommendation would be to take out the Garbage Collector calls. You are telling the GC to perform a collection which takes some time. You really don't need to call a collection since you already dispose of your objects. A full collection is call automatically by the GC when you try to allocate new memory and the heap or allocated space is full.
Akmal Ali16 reputationPosted Feb 15, 2011, 4:13 AM EST
0
If I remove this code GC.Collect(); GC.WaitForPendingFinalizers();
,the program willstopwith the exeption -"Out of memory". You can to try it.
0
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 15, 2011, 4:19 AM EST
0
Akmal, do you think you could uploaded your solution. I think there is an issue somewhere that is causing the memory to run out, but not related to calling the GC.
How many times do you call that function?
If you call it more than once?
-Check if the Image in the PictureBox != null and call Dispose on it before
giving it a new reference.
0
FroglegRank #1,020 · 1.9K reputationPosted Feb 15, 2011, 9:14 AM EST
0
To load an image you only need this
private void button1_Click(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile(@"c:\MAP_TAS\wash.jpg"); }
Akmal Ali16 reputationPosted Feb 15, 2011, 6:41 PM EST
0
to Fellipe Ramos. I suggest youcreatea VS2008 C# "Windows Form" projectandinsertmycodethere.Lines - "GC.Collect(); GC.WaitForPendingFinalizers();"-comment out.Run theprogram,a windowmaximizedandto tryto move with the mousethe imageinside a windowfromone sideto another.Tobe betterunderstood, Iproposeto launch "task manager"andseehowyourprogram willconsumememory. Check it please, it does not take so much time. Image better take a biger size. Sorry for my English.
0
FroglegRank #1,020 · 1.9K reputationPosted Feb 15, 2011, 6:45 PM EST
Akmal Ali16 reputationPosted Feb 15, 2011, 9:50 PM EST
0
By default, theimageis placedinto the "PictureBox" to x=0 and y=0. Butsometimes it is necessaryto put animageat x=10 and y=20for example. How can you do it?
0
Sam HobbsRank #107 · 20.5K reputationPosted Feb 15, 2011, 11:10 PM EST
0
I see you are still calling Dispose so either my advice (in your previous thread) is wrong or it is a waste of my time to try to help.
In case it helps, I will say that you need to be careful about processing the mouse move message (event). That message is the most frequent message in Windows, or at least very close to being the most frequent. You can easily use up a lot of processor time just by processing that message (event).
0
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 16, 2011, 3:16 AM EST
0
Akmal, I created the solution and perform some benchmarks with the code how you have it using the GC calls. I did see the spike when talking those calls out. I re-wrote the code for the draw function and got the same memory usage that you get with the calls to GC. Try it out and let me know if it helps:
private void draw_map(int x, int y)
{
try
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
using (Bitmap tbmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
using (Graphics tgr = Graphics.FromImage(tbmp))
{
tgr.DrawImage(map, x, y);
Rectangle rct = new Rectangle(0, 0, tbmp.Width, tbmp.Height);
Image img = tbmp.Clone(rct, tbmp.PixelFormat);
pictureBox1.Image = img;
}
}
px = x;
py = y;
}
catch (OutOfMemoryException outOfMemoryException)
{
// Add Handling
}
catch (Exception ex)
{
// Add Handling
}
}
0
FroglegRank #1,020 · 1.9K reputationPosted Feb 16, 2011, 7:24 AM EST
0
Are you trying to scroll image ? If so, put a panel on your form with the picturebox inside it. In the panel propertieis - set AutoScroll to true In picturebox properties - set SizeMode to AutoSize
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 15, 2011, 3:34 AM EST
Akmal Ali16 reputationPosted Feb 15, 2011, 4:13 AM EST
GC.Collect();
GC.WaitForPendingFinalizers();
, the program will stop with the exeption - "Out of memory".
You can to try it.
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 15, 2011, 4:19 AM EST
FroglegRank #1,020 · 1.9K reputationPosted Feb 15, 2011, 9:14 AM EST
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"c:\MAP_TAS\wash.jpg");
}
Akmal Ali16 reputationPosted Feb 15, 2011, 6:29 PM EST
And if you need to place the image at the desired point, but not where it will be the default?
Akmal Ali16 reputationPosted Feb 15, 2011, 6:41 PM EST
I suggest you create a VS2008 C# "Windows Form" project and insert my code there. Lines - "GC.Collect();
GC.WaitForPendingFinalizers();"- comment out. Run the program, a window maximized and to try to move with the mouse the image inside a window from one side to another. To be better understood, I propose to launch "task manager" and see how your program will consume memory.
Check it please, it does not take so much time. Image better take a biger size.
Sorry for my English.
FroglegRank #1,020 · 1.9K reputationPosted Feb 15, 2011, 6:45 PM EST
Akmal Ali16 reputationPosted Feb 15, 2011, 9:50 PM EST
But sometimes it is necessary to put an image at x=10 and y=20 for example.
How can you do it?
Sam HobbsRank #107 · 20.5K reputationPosted Feb 15, 2011, 11:10 PM EST
In case it helps, I will say that you need to be careful about processing the mouse move message (event). That message is the most frequent message in Windows, or at least very close to being the most frequent. You can easily use up a lot of processor time just by processing that message (event).
Felipe RamosRank #1,409 · 1.2K reputationPosted Feb 16, 2011, 3:16 AM EST
FroglegRank #1,020 · 1.9K reputationPosted Feb 16, 2011, 7:24 AM EST
If so, put a panel on your form with the picturebox inside it.
In the panel propertieis - set AutoScroll to true
In picturebox properties - set SizeMode to AutoSize