在 picturebox c# 中裁剪和加载平铺图像
Crop and load tiled image in picturebox c#
我有一个像 https://i.imgsafe.org/67397f9.png
这样的平铺图像,我想在某些鼠标事件中将其部分作为图片框的图像加载。实际上我想要模拟按钮行为。
Bitmap source = new Bitmap(Properties.Resources.btn_close);
public Bitmap CropImage(Bitmap srcbmp, Rectangle dstcrp)
{
Bitmap bmp = new Bitmap(dstcrp.Width, dstcrp.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.DrawImage(srcbmp, 0, 0, dstcrp, GraphicsUnit.Pixel);
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 62), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
这是我的代码,用于裁剪图像部分并加载为图片框的位图。
我认为它不是很专业,可能存在一些内存使用问题等......
有什么简单的方法可以做到这一点吗?
2个我能想到的方案
当您第一次加载表单时,您可以为每个鼠标状态声明 4 image/bitmap 个变量 1
鼠标按下、离开和回车
因此,您无需一次又一次地重新创建图像,您只需在适当的时候更改图像即可。
var cropCoordinates= new Rectangle(new Point(0, 0), new Size(51, 30));
var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);
您可以为每个状态设置 4 个不同的图像框(第一个位于另一个之上)并在需要时显示或隐藏
我有一个像 https://i.imgsafe.org/67397f9.png
这样的平铺图像,我想在某些鼠标事件中将其部分作为图片框的图像加载。实际上我想要模拟按钮行为。
Bitmap source = new Bitmap(Properties.Resources.btn_close);
public Bitmap CropImage(Bitmap srcbmp, Rectangle dstcrp)
{
Bitmap bmp = new Bitmap(dstcrp.Width, dstcrp.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.DrawImage(srcbmp, 0, 0, dstcrp, GraphicsUnit.Pixel);
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 62), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
这是我的代码,用于裁剪图像部分并加载为图片框的位图。 我认为它不是很专业,可能存在一些内存使用问题等...... 有什么简单的方法可以做到这一点吗?
2个我能想到的方案
当您第一次加载表单时,您可以为每个鼠标状态声明 4 image/bitmap 个变量 1
鼠标按下、离开和回车
因此,您无需一次又一次地重新创建图像,您只需在适当的时候更改图像即可。var cropCoordinates= new Rectangle(new Point(0, 0), new Size(51, 30));
var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);您可以为每个状态设置 4 个不同的图像框(第一个位于另一个之上)并在需要时显示或隐藏