如何在 strechmode-c# 的图片框中裁剪和保存图像
how to crop and save an image in a picturebox in strechmode-c#
我无法在 strechmode 的图片框中裁剪图像,我尝试了数周但仍未找到解决方案。问题是我的图像大小是(1200,750),图片大小是(450,300),但是当我尝试在 strechmode 的图片框中裁剪它时,它采用图像原始大小的坐标(1200,750),但我需要使用 picturebox(450,300) 中显示的图像坐标进行裁剪,因此我在裁剪时得到了错误的结果。我在 c# winforms 中使用 visual studio 2013。请给我一个解决方案。先感谢您。
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;
// Redraw the picturebox:
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// Make a note that we "have the mouse".
bHaveMouse = true;
// Store the "starting point" for this rubber-band rectangle.
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
// Special value lets us know that no previous
// rectangle needs to be erased.
// Display coordinates
//lbCordinates.Text = "Coordinates : " + e.X.ToString() + ", " + e.Y.ToString();
//ptLast.X = -1;
//ptLast.Y = -1;
//rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());
if (e.Button == MouseButtons.Left && _selecting)
{
// Create cropped image:
Image img = pictureBox1.Image.Crop(_selection);
// Fit image to the picturebox:
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = img.Fit2PictureBox(pictureBox1);
_selecting = false;
}
button5.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_selecting)
{
// Draw a rectangle displaying the current selection
Pen pen = Pens.GreenYellow;
e.Graphics.DrawRectangle(pen, _selection);
}
}
`我使用的命名空间class如下
namespace gfoidl.Imaging
{
public static class ImageExtension
{
public static Image Crop(this Image image, Rectangle selection)
{
Bitmap bmp = image as Bitmap;
// Check if it is a bitmap:
if (bmp == null)
throw new ArgumentException("Kein gültiges Bild (Bitmap)");
// Crop the image:
Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);
// Release the resources:
image.Dispose();
return cropBmp;
}
public static Image Fit2PictureBox( this Image image, PictureBox picBox)
{
Bitmap bmp = null;
Graphics g;
// Scale:
double scaleY = (double)image.Width / picBox.Width;
double scaleX = (double)image.Height / picBox.Height;
double scale = scaleY < scaleX ? scaleX : scaleY;
// Create new bitmap:
bmp = new Bitmap(
(int)((double)image.Width / scale),
(int)((double)image.Height / scale));
// Set resolution of the new image:
bmp.SetResolution(
image.HorizontalResolution,
image.VerticalResolution);
// Create graphics:
g = Graphics.FromImage(bmp);
// Set interpolation mode:
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new image:
g.DrawImage(
image,
new Rectangle( // Ziel
0, 0,
bmp.Width, bmp.Height),
new Rectangle( // Quelle
0, 0,
image.Width, image.Height),
GraphicsUnit.Pixel);
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
// Release the resources of the graphics:
g.Dispose();
// Release the resources of the origin image:
image.Dispose();
return bmp;
}
}
} //invoked it in the main program with the header using gfoid1.imaging;
如果你想在不离开picbox的情况下全方位选择:
private Point mouseDown;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
mouseDown = new Point(e.X, e.Y);
_selection = new Rectangle(mouseDown, new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_selecting)
{
Point mousePos = new Point(e.X, e.Y);
if(mousePos.X < 0)
{
mousePos.X = 0;
}
if(mousePos.X >= pictureBox1.Width)
{
mousePos.X = pictureBox1.Width - 1;
}
if(mousePos.Y < 0)
{
mousePos.Y = 0;
}
if (mousePos.Y >= pictureBox1.Height)
{
mousePos.Y = pictureBox1.Height - 1;
}
_selection.X = Math.Min(mouseDown.X, mousePos.X);
_selection.Y = Math.Min(mouseDown.Y, mousePos.Y);
_selection.Width = Math.Abs(mousePos.X - mouseDown.X);
_selection.Height = Math.Abs(mousePos.Y - mouseDown.Y);
// Redraw the picturebox:
pictureBox1.Invalidate();
}
}
private Point mouseDown;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
mouseDown = new Point(e.X, e.Y);
_selection = new Rectangle(mouseDown, new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_selecting)
{
Point mousePos = new Point(e.X, e.Y);
if(mousePos.X < 0)
{
mousePos.X = 0;
}
if(mousePos.X >= pictureBox1.Width)
{
mousePos.X = pictureBox1.Width - 1;
}
if(mousePos.Y < 0)
{
mousePos.Y = 0;
}
if (mousePos.Y >= pictureBox1.Height)
{
mousePos.Y = pictureBox1.Height - 1;
}
_selection.X = Math.Min(mouseDown.X, mousePos.X);
_selection.Y = Math.Min(mouseDown.Y, mousePos.Y);
_selection.Width = Math.Abs(mousePos.X - mouseDown.X);
_selection.Height = Math.Abs(mousePos.Y - mouseDown.Y);
// Redraw the picturebox:
pictureBox1.Invalidate();
}
}
我无法在 strechmode 的图片框中裁剪图像,我尝试了数周但仍未找到解决方案。问题是我的图像大小是(1200,750),图片大小是(450,300),但是当我尝试在 strechmode 的图片框中裁剪它时,它采用图像原始大小的坐标(1200,750),但我需要使用 picturebox(450,300) 中显示的图像坐标进行裁剪,因此我在裁剪时得到了错误的结果。我在 c# winforms 中使用 visual studio 2013。请给我一个解决方案。先感谢您。
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;
// Redraw the picturebox:
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// Make a note that we "have the mouse".
bHaveMouse = true;
// Store the "starting point" for this rubber-band rectangle.
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
// Special value lets us know that no previous
// rectangle needs to be erased.
// Display coordinates
//lbCordinates.Text = "Coordinates : " + e.X.ToString() + ", " + e.Y.ToString();
//ptLast.X = -1;
//ptLast.Y = -1;
//rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());
if (e.Button == MouseButtons.Left && _selecting)
{
// Create cropped image:
Image img = pictureBox1.Image.Crop(_selection);
// Fit image to the picturebox:
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = img.Fit2PictureBox(pictureBox1);
_selecting = false;
}
button5.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_selecting)
{
// Draw a rectangle displaying the current selection
Pen pen = Pens.GreenYellow;
e.Graphics.DrawRectangle(pen, _selection);
}
}
`我使用的命名空间class如下
namespace gfoidl.Imaging
{
public static class ImageExtension
{
public static Image Crop(this Image image, Rectangle selection)
{
Bitmap bmp = image as Bitmap;
// Check if it is a bitmap:
if (bmp == null)
throw new ArgumentException("Kein gültiges Bild (Bitmap)");
// Crop the image:
Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);
// Release the resources:
image.Dispose();
return cropBmp;
}
public static Image Fit2PictureBox( this Image image, PictureBox picBox)
{
Bitmap bmp = null;
Graphics g;
// Scale:
double scaleY = (double)image.Width / picBox.Width;
double scaleX = (double)image.Height / picBox.Height;
double scale = scaleY < scaleX ? scaleX : scaleY;
// Create new bitmap:
bmp = new Bitmap(
(int)((double)image.Width / scale),
(int)((double)image.Height / scale));
// Set resolution of the new image:
bmp.SetResolution(
image.HorizontalResolution,
image.VerticalResolution);
// Create graphics:
g = Graphics.FromImage(bmp);
// Set interpolation mode:
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new image:
g.DrawImage(
image,
new Rectangle( // Ziel
0, 0,
bmp.Width, bmp.Height),
new Rectangle( // Quelle
0, 0,
image.Width, image.Height),
GraphicsUnit.Pixel);
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
// Release the resources of the graphics:
g.Dispose();
// Release the resources of the origin image:
image.Dispose();
return bmp;
}
}
} //invoked it in the main program with the header using gfoid1.imaging;
如果你想在不离开picbox的情况下全方位选择:
private Point mouseDown;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
mouseDown = new Point(e.X, e.Y);
_selection = new Rectangle(mouseDown, new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_selecting)
{
Point mousePos = new Point(e.X, e.Y);
if(mousePos.X < 0)
{
mousePos.X = 0;
}
if(mousePos.X >= pictureBox1.Width)
{
mousePos.X = pictureBox1.Width - 1;
}
if(mousePos.Y < 0)
{
mousePos.Y = 0;
}
if (mousePos.Y >= pictureBox1.Height)
{
mousePos.Y = pictureBox1.Height - 1;
}
_selection.X = Math.Min(mouseDown.X, mousePos.X);
_selection.Y = Math.Min(mouseDown.Y, mousePos.Y);
_selection.Width = Math.Abs(mousePos.X - mouseDown.X);
_selection.Height = Math.Abs(mousePos.Y - mouseDown.Y);
// Redraw the picturebox:
pictureBox1.Invalidate();
}
}
private Point mouseDown;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
mouseDown = new Point(e.X, e.Y);
_selection = new Rectangle(mouseDown, new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_selecting)
{
Point mousePos = new Point(e.X, e.Y);
if(mousePos.X < 0)
{
mousePos.X = 0;
}
if(mousePos.X >= pictureBox1.Width)
{
mousePos.X = pictureBox1.Width - 1;
}
if(mousePos.Y < 0)
{
mousePos.Y = 0;
}
if (mousePos.Y >= pictureBox1.Height)
{
mousePos.Y = pictureBox1.Height - 1;
}
_selection.X = Math.Min(mouseDown.X, mousePos.X);
_selection.Y = Math.Min(mouseDown.Y, mousePos.Y);
_selection.Width = Math.Abs(mousePos.X - mouseDown.X);
_selection.Height = Math.Abs(mousePos.Y - mouseDown.Y);
// Redraw the picturebox:
pictureBox1.Invalidate();
}
}