简体   繁体   English

如何重新调整图像C#.net Windows窗体的大小

[英]How To Re-size An Image C#.net Windows Forms

I was wondering, once i load an image into a windows form, is there a way for me to allow the user to drag the corners of that image and re-size it? 我想知道,一旦我将图像加载到窗体中,有没有办法允许用户拖动该图像的角落并重新调整大小?

Currently, i know about the PictureBox.Scale method (but this is deprecated). 目前,我知道PictureBox.Scale方法(但不推荐使用)。 I also know about PictureBox.Image.Size. 我也知道PictureBox.Image.Size。 Would this mean that each time they re-size i would need to use PictureBox.Image.Size? 这是否意味着每次重新调整大小时我都需要使用PictureBox.Image.Size? Also, how do i allow them to grab the image for re-sizing? 另外,我如何允许他们抓取图像以重新调整大小? I guess i'm thinking about paint and how it allows the user to select the image and then re-size it by dragging the corners... 我想我正在考虑绘画以及它如何允许用户选择图像,然后通过拖动角来重新调整大小......

I'm not looking for a full solution - just some pointers in the right direction (Pseudo code or general description to help my thought process would be fine). 我不是在寻找一个完整的解决方案 - 只是在正确方向上的一些指针(伪代码或一般描述来帮助我的思维过程会很好)。 I'm not quite sure how to approach this problem. 我不太确定如何解决这个问题。

Here is my code so far: 到目前为止,这是我的代码:

        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Title = "Load Image";

            if (ofd.ShowDialog() == DialogResult.OK)
            {

                PictureBox pictureBox = new PictureBox();
                pictureBox.Image = new Bitmap(ofd.FileName);
                pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox.Size = pictureBox.Image.Size; 
                panelArea.Controls.Add(pictureBox); 

            }
        }

There is already a control in Winforms that can display a bitmap and supports resizing with the mouse: a form. Winforms中已经有一个控件可以显示位图并支持使用鼠标调整大小:表单。 You just need a little bit of surgery to turn it into a control: 你只需要一点手术就可以把它变成一个控制:

        using (OpenFileDialog ofd = new OpenFileDialog()) {
            ofd.Title = "Load Image";

            if (ofd.ShowDialog() == DialogResult.OK) {
                var box = new Form();
                box.TopLevel = box.ControlBox = false;
                box.Visible = true;
                box.BackgroundImage = new Bitmap(ofd.FileName);
                panelArea.Controls.Add(box);
                box.Size = box.BackgroundImage.Size;
            }
        }

Edit : In your case, Hans Passant's method is best. 编辑 :在你的情况下,Hans Passant的方法是最好的。 I'll keep my answer up in case you eventually have more complex editing requirements (eg, dragging multiple shapes) 我会保留我的答案,以防你最终有更复杂的编辑要求(例如,拖动多个形状)


The interesting part is getting the mouse interaction to work. 有趣的是让鼠标交互起作用。 For now, let's assume you only want to resize by dragging the lower-right hand corner of the image. 现在,让我们假设您只想通过拖动图像的右下角来调整大小。 I would do something like this: 我会做这样的事情:

  1. Create a UserControl named, eg, ImageCanvas . 创建一个名为的UserControl,例如ImageCanvas Inside this control, do the following: 在此控件内,执行以下操作:

  2. Add a field to keep track of the size/location of the image, perhaps Rectangle imageRect 添加一个字段以跟踪图像的大小/位置,可能是Rectangle imageRect

  3. Override the OnPaint method to display your image: 覆盖OnPaint方法以显示图像:

      protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(image, imageRect); } 
  4. Add a field to keep track of when the user is resizing the image, say bool isDraggingSize 添加一个字段以跟踪用户何时调整图像大小,例如bool isDraggingSize

  5. Override OnMouseDown , and OnMouseUp . 覆盖OnMouseDownOnMouseUp Set or clear isDraggingSize when the mouse button goes down or up, respectively. 当鼠标按钮分别向下或向上时,设置或清除isDraggingSize

  6. Override OnMouseMove to do the following: 覆盖OnMouseMove以执行以下操作:

    • Set the cursor: If isDraggingSize or the mouse pointer is close to the lower right corner of the image, set Cursor = Cursors.SizeNWSE ; 设置光标:如果isDraggingSize或鼠标指针靠近图像的右下角,则设置Cursor = Cursors.SizeNWSE ; otherwise, set Cursor = Cursors.Default . 否则,设置Cursor = Cursors.Default

    • If isDraggingSize , change the imageSize field based on the mouse location. 如果是isDraggingSize ,请根据鼠标位置更改imageSize字段。 Call Invalidate() , or Refresh() if necessary, to update the display. 如有必要,调用Invalidate()Refresh()来更新显示。

  7. Play around with it, and fix all the little details -- like deciding what happens when the user tries to resize the image larger than the control, and how to get rid of flickering. 玩它,并修复所有的小细节 - 比如决定当用户试图调整大于控件的图像时会发生什么,以及如何摆脱闪烁。

 public Bitmap _currentBitmap;

 public void Resize(int newWidth, int newHeight)
    {
        if (newWidth != 0 && newHeight != 0)
        {
            Bitmap temp = (Bitmap)_currentBitmap;
            Bitmap bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);

            double nWidthFactor = (double)temp.Width / (double)newWidth;
            double nHeightFactor = (double)temp.Height / (double)newHeight;

            double fx, fy, nx, ny;
            int cx, cy, fr_x, fr_y;
            Color color1 = new Color();
            Color color2 = new Color();
            Color color3 = new Color();
            Color color4 = new Color();
            byte nRed, nGreen, nBlue;

            byte bp1, bp2;

            for (int x = 0; x < bmap.Width; ++x)
            {
                for (int y = 0; y < bmap.Height; ++y)
                {

                    fr_x = (int)Math.Floor(x * nWidthFactor);
                    fr_y = (int)Math.Floor(y * nHeightFactor);
                    cx = fr_x + 1;
                    if (cx >= temp.Width) cx = fr_x;
                    cy = fr_y + 1;
                    if (cy >= temp.Height) cy = fr_y;
                    fx = x * nWidthFactor - fr_x;
                    fy = y * nHeightFactor - fr_y;
                    nx = 1.0 - fx;
                    ny = 1.0 - fy;

                    color1 = temp.GetPixel(fr_x, fr_y);
                    color2 = temp.GetPixel(cx, fr_y);
                    color3 = temp.GetPixel(fr_x, cy);
                    color4 = temp.GetPixel(cx, cy);

                    // Blue
                    bp1 = (byte)(nx * color1.B + fx * color2.B);

                    bp2 = (byte)(nx * color3.B + fx * color4.B);

                    nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                    // Green
                    bp1 = (byte)(nx * color1.G + fx * color2.G);

                    bp2 = (byte)(nx * color3.G + fx * color4.G);

                    nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                    // Red
                    bp1 = (byte)(nx * color1.R + fx * color2.R);

                    bp2 = (byte)(nx * color3.R + fx * color4.R);

                    nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                    bmap.SetPixel(x, y, System.Drawing.Color.FromArgb(255, nRed, nGreen, nBlue));
                }
            }
            _currentBitmap = (Bitmap)bmap.Clone();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据控件可见性C#.Net动态自动调整窗口大小 - Auto re-size windows form dynamically according to the control visibility, C#.Net 如何使用asp.net C#调整图像大小而不保存图像 - How to re-size an image with asp.net C# without saving it C#.NET-如何从现有的Windows窗体控件继承 - C#.NET - How to inherit from an existing windows forms control 在C#.NET和Windows窗体中将照片转换为灰度 - Photo to grayscale in C#.NET and Windows Forms 如何在不损失分辨率的情况下重新调整QRCode图像的大小? - How to re-size QRCode image without losing resolution? 如何在C#中调整窗体的大小以包含其控件? - How to re-size a form to contain its controls in C#? 加速在Windows窗体(c#.net)应用程序中从磁盘加载图像 - Speed up loading an image from disk in a windows forms (c#.net) app 为了获得最佳效果,您是否应该重新调整图像大小然后对其进行压缩,还是应该对其进行压缩然后重新调整大小? - For best results, should you re-size an image and then compress it or should you compress it and then re-size it? 如何更改我的treeView图标+, - 像c#.net win窗体中的Windows资源管理器树视图 - How to change my treeView icons insted of +,- like a windows explorer treeview in c#.net win forms 如何在C#.net(2010)Windows窗体应用程序中获取TreeView控件中的所有复选框值? - how to get the All the Checkbox values in TreeView control in C#.net(2010) Windows Forms Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM