【问题标题】:Crop and load tiled image in picturebox c#在picturebox c#中裁剪和加载平铺图像
【发布时间】:2016-03-22 18:30:00
【问题描述】:

我有一个像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);
    }

这是我的代码,用于裁剪图像部分并加载为图片框的位图。 我觉得不是很专业,可能有一些内存使用问题等等…… 有什么简单的方法吗?

【问题讨论】:

    标签: c# image button picturebox


    【解决方案1】:

    我能想到的2个解决方案

    1. 当您第一次加载表单时,您可以为每个鼠标状态声明 4 个图像/位图变量 1
      鼠标按下、离开和输入
      这样就不必再次重新创建图像并再次,您可以在适当的时候更改图像。

      var cropCoordinates= new Rectangle(new Point(0, 0), new Size(51, 30));
      var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);

    2. 您可以为每个状态设置 4 个不同的图像框(将 1 层叠加在一起),并在需要时显示或隐藏

    【讨论】:

    • 谢谢!有没有为此目的的任何组件?即在其上设置一些属性,例如 OnMouseMoveImage or OnMouseDownImage , etc... 无需编码?
    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多