[英]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:
我会做这样的事情:
Create a UserControl named, eg, ImageCanvas
. 创建一个名为的UserControl,例如
ImageCanvas
。 Inside this control, do the following: 在此控件内,执行以下操作:
Add a field to keep track of the size/location of the image, perhaps Rectangle imageRect
添加一个字段以跟踪图像的大小/位置,可能是
Rectangle imageRect
Override the OnPaint
method to display your image: 覆盖
OnPaint
方法以显示图像:
protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(image, imageRect); }
Add a field to keep track of when the user is resizing the image, say bool isDraggingSize
添加一个字段以跟踪用户何时调整图像大小,例如
bool isDraggingSize
Override OnMouseDown
, and OnMouseUp
. 覆盖
OnMouseDown
和OnMouseUp
。 Set or clear isDraggingSize
when the mouse button goes down or up, respectively. 当鼠标按钮分别向下或向上时,设置或清除
isDraggingSize
。
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()
来更新显示。
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.