Good day to all.
Please see my 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; private void button1_Click(object sender, EventArgs e) { map = Image.FromFile(@"c:\MAP_TAS\wash.jpg"); draw_map(0, 0); } private void draw_map(int x, int y) { pictureBox1.Image = null; Bitmap tbmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 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; tgr.Dispose(); tbmp.Dispose(); px = x; py = y; GC.Collect(); GC.WaitForPendingFinalizers(); } int mx, my, cmx, cmy, px, py; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mx = e.X; my = e.Y; label1.Text = "mx = " + mx.ToString(); label2.Text = "my = " + my.ToString();
} } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { cmx = e.X - mx; cmy = e.Y - my;
px = px + cmx; py = py + cmy;
draw_map(px, py); cmx = 0; cmy = 0; mx = e.X; my = e.Y; label3.Text = "cmx = " + cmx.ToString(); label4.Text = "cmy = " + cmy.ToString(); Cursor = Cursors.SizeAll; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Cursor = Cursors.Default; cmx = 0; cmy = 0; } }
} }
|
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.
FroglegPosted Feb 16, 2011, 12:24 PM
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 RamosPosted Feb 16, 2011, 8:16 AM
Sam HobbsPosted Feb 16, 2011, 4:10 AM
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).
Akmal AliPosted Feb 16, 2011, 2:50 AM
But sometimes it is necessary to put an image at x=10 and y=20 for example.
How can you do it?
FroglegPosted Feb 15, 2011, 11:45 PM
Akmal AliPosted Feb 15, 2011, 11:41 PM
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.
Akmal AliPosted Feb 15, 2011, 11:29 PM
And if you need to place the image at the desired point, but not where it will be the default?
FroglegPosted Feb 15, 2011, 2:14 PM
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"c:\MAP_TAS\wash.jpg");
}
Felipe RamosPosted Feb 15, 2011, 9:19 AM
Akmal AliPosted Feb 15, 2011, 9:13 AM
GC.Collect();
GC.WaitForPendingFinalizers();
, the program will stop with the exeption - "Out of memory".
You can to try it.
Felipe RamosPosted Feb 15, 2011, 8:34 AM