我有像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);
}
这是我的代码,用于裁剪图像部分并加载为图片框的位图。 我认为它不是很专业,可能有一些内存使用问题,等等...... 有没有简单的方法呢?
答案 0 :(得分:0)
我能想到的2个解决方案
首次加载表单时,您可以为每个鼠标状态声明4个图像/位图变量1 鼠标按下,保持向上并输入
而不是一次又一次地重新调整图像,您可以在时间合适时更改图像。
var cropCoordinates = new Rectangle(new Point(0,0),new Size(51,30));
var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);
每种状态可以有4个不同的图像框(在另一个上面分层1)并在需要时显示或隐藏