我試圖實現圖像編輯器,我想在圖片框上的現有圖像上繪製圖像。我已經使用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