我试图实现图像编辑器,我想在图片框上的现有图像上绘制图像。我已经使用Graphics.DrawImage并使用MouseEventArguments点实现了它。但问题是我想要在绘制图像顶部绘制一个选择矩形,当鼠标点击它时,它应该重绘在新获得的位置上点击并拖动鼠标(没有选择矩形)。图像在新位置的渲染缓慢且消耗大量内存,所以我使用GC.collect.Please帮助我实现选择。我发布了我已经做到现在以下通过捕获MouseUp和MouseMove在图片框中的图像上绘制图像(拖动并绘制图像)
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;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication32
{
public partial class Form1 : Form
{
Bitmap timg, timg2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timg = new Bitmap(pictureBox1.Image);
timg2 = new Bitmap(pictureBox1.Image);
pictureBox1.MouseDown+=new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove+=new MouseEventHandler(pictureBox1_MouseMove);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button ==MouseButtons.Left)
{
timg = new Bitmap(timg2);
Graphics g = Graphics.FromImage(timg);
g.DrawImage(pictureBox2.Image, new Point(e.X, e.Y));
pictureBox1.Image = timg;
g.Dispose();
GC.Collect();
//pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawImage(pictureBox2.Image, new Point(e.X, e.Y));
// pictureBox1.Invalidate();
g.Dispose();
GC.Collect();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
设置公共属性,并在窗体加载图形g = Graphics.FromImage(timg); – 2011-12-31 20:35:37