Akmal Ali

Akmal Ali

  • NA
  • 16
  • 29.6k

One more question from me. Why it works so slow?

Feb 15 2011 3:27 AM

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.

Answers (11)