C#-强制矩形保持固定大小/比例
与Photoshop类似,在Photoshop中,您可以键入一个比率,或者在我的例子中键入一个特定的维度,例如800x600,我希望在拖动鼠标时能够强制将一个C#-强制矩形保持固定大小/比例,c#,visual-studio,crop,picturebox,drawrectangle,C#,Visual Studio,Crop,Picturebox,Drawrectangle,与Photoshop类似,在Photoshop中,您可以键入一个比率,或者在我的例子中键入一个特定的维度,例如800x600,我希望在拖动鼠标时能够强制将一个矩形设置为固定的比率/大小 目前,我有以下几点: 它将使用通过单击并拖动图片框创建的矩形来裁剪图像。边界框选择没有任何约束的区域。我希望能够强制矩形具有一定的比例(最好是从设定的分辨率),类似于Photoshop的裁剪工具的工作方式 我的消息来源如果有人需要更多详细信息: 表格1.cs using System; using System
矩形
设置为固定的比率/大小
目前,我有以下几点:
它将使用通过单击并拖动图片框
创建的矩形来裁剪图像。边界框选择没有任何约束的区域。我希望能够强制矩形具有一定的比例(最好是从设定的分辨率),类似于Photoshop的裁剪工具的工作方式
我的消息来源如果有人需要更多详细信息:
表格1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CropResize
{
public partial class Form1 : Form
{
private static string path;
public Form1(string filePath)
{
InitializeComponent();
path = filePath;
}
private Image _originalImage;
private Image _newImage;
private bool _selecting;
private Rectangle _selection;
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.Image = Image.FromFile(path);
if (pictureBox1.Image.Height > Screen.PrimaryScreen.Bounds.Height - 50 || pictureBox1.Image.Width > Screen.PrimaryScreen.Bounds.Width - 50)
{
if (pictureBox1.Image.Height > Screen.PrimaryScreen.Bounds.Height - 50)
{
Height = Screen.PrimaryScreen.Bounds.Height - 50;
panel1.Height = Size.Height - statusStrip1.Height - buttonSave.Height - 60;
}
if (pictureBox1.Image.Width > Screen.PrimaryScreen.Bounds.Width - 50)
{
Size = new Size(Screen.PrimaryScreen.Bounds.Width - 50, Screen.PrimaryScreen.Bounds.Height - 50);
panel1.Width = Size.Width - statusStrip1.Height - buttonSave.Height - 60;
}
pictureBox1.Image = pictureBox1.Image.Fit2PictureBox(pictureBox1);
panel1.Size = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
}
Size = new Size(panel1.Size.Width + 50, panel1.Size.Height + buttonSave.Height + statusStrip1.Height + 80);
// Create a copy of the original image for later use
_originalImage = pictureBox1.Image.Clone() as Image;
_newImage = pictureBox1.Image.Clone() as Image;
}
private void buttonOrig_Click(object sender, System.EventArgs e)
{
pictureBox1.Image = _originalImage.Clone() as Image;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = _originalImage.Clone() as Image;
_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);
//int nGCD = GetGreatestCommonDivisor(1920, 1080);
//_selection.Width = _selection.Width / nGCD;
//_selection.Height = _selection.Height / nGCD;
int widthRatio = 16;
int heightRatio = 9;
if (_selection.Height * widthRatio <= _selection.Width)
{
_selection.Width = _selection.Height * widthRatio;
}
else if (_selection.Width * heightRatio <= _selection.Width)
{
_selection.Height = _selection.Width * heightRatio;
}
// Redraw the picturebox:
pictureBox1.Refresh();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_selecting && _selection.Height != 0)
{
// Draw a rectangle displaying the current selection
e.Graphics.DrawRectangle(Pens.WhiteSmoke, _selection);
//e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Gray)), 0, pictureBox1.Height - pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image.Height);
e.Graphics.SetClip(_selection, CombineMode.Exclude);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.Black)), 0, 0, pictureBox1.Width, pictureBox1.Height);
int nGCD = GetGreatestCommonDivisor(_selection.Width, _selection.Height);
string str = string.Format("{0}:{1}", _selection.Width / nGCD, _selection.Height / nGCD);
toolStripStatusLabel1.Text = "Image Size: " + _selection.Width + "x" + _selection.Height + "px. Aspect Ratio: " + str;
}
}
public static int GetGreatestCommonDivisor(int height, int width)
{
return width == 0 ? height : GetGreatestCommonDivisor(width, height % width);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left &&
_selecting &&
_selection.Size != new Size())
{
// Create cropped image:
_newImage = pictureBox1.Image.Crop(_selection);
_selecting = false;
try
{
// Set new image to the picturebox:
//pictureBox1.Image = _newImage.Fit2PictureBox(pictureBox1);
pictureBox1.Image = _newImage;
//toolStripStatusLabel1.Text = "Image Cropped.";
}
catch (Exception)
{ }
}
else
{
_selecting = false;
}
}
private void buttonResize_Click(object sender, EventArgs e)
{
pictureBox1.Image = ResizeImage(pictureBox1.Image, new Size(800, 600));
int nGCD = GetGreatestCommonDivisor(pictureBox1.Image.Width, pictureBox1.Image.Height);
string str = string.Format("{0}:{1}", pictureBox1.Image.Width / nGCD, pictureBox1.Image.Height / nGCD);
toolStripStatusLabel1.Text = "Image Resized to " + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + "px. Aspect Ratio: " + str;
}
public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true)
{
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = size.Width / originalWidth;
float percentHeight = size.Height / originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
private void buttonSave_Click(object sender, EventArgs e)
{
string filename = path.Substring(path.LastIndexOf("\\") + 1);
string newPath = path.Substring(0, path.LastIndexOf(".") - 1) + "NEW.png";
toolStripStatusLabel1.Text = "Saving " + filename + " to " + newPath;
pictureBox1.Image.Save(newPath, ImageFormat.Png);
toolStripStatusLabel1.Text = filename + " saved to " + newPath;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Drawing.Drawing2D;
使用系统、绘图、成像;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间CropResize
{
公共部分类Form1:Form
{
私有静态字符串路径;
公共表单1(字符串文件路径)
{
初始化组件();
路径=文件路径;
}
私人形象(原创图像),;
私人形象(newImage);;
私人住宅选择;
私有矩形_选择;
私有void Form1_加载(对象发送方,System.EventArgs e)
{
pictureBox1.Image=Image.FromFile(路径);
if(pictureBox1.Image.Height>Screen.PrimaryScreen.Bounds.Height-50 | | pictureBox1.Image.Width>Screen.PrimaryScreen.Bounds.Width-50)
{
if(pictureBox1.Image.Height>Screen.PrimaryScreen.Bounds.Height-50)
{
高度=Screen.PrimaryScreen.Bounds.Height-50;
panel1.Height=Size.Height-statusStrip1.Height-buttonSave.Height-60;
}
if(pictureBox1.Image.Width>Screen.PrimaryScreen.Bounds.Width-50)
{
大小=新大小(Screen.PrimaryScreen.Bounds.Width-50,Screen.PrimaryScreen.Bounds.Height-50);
panel1.Width=Size.Width-statusStrip1.Height-buttonSave.Height-60;
}
pictureBox1.Image=pictureBox1.Image.Fit2PictureBox(pictureBox1);
panel1.Size=新尺寸(pictureBox1.Image.Width,pictureBox1.Image.Height);
}
尺寸=新尺寸(panel1.Size.Width+50,panel1.Size.Height+buttonSave.Height+statusStrip1.Height+80);
//创建原始图像的副本供以后使用
_originalImage=pictureBox1.Image.Clone()作为图像;
_newImage=pictureBox1.Image.Clone()作为图像;
}
私有无效按钮单击(对象发送者,System.EventArgs e)
{
pictureBox1.Image=\u originalImage.Clone()作为图像;
}
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
//选择的起点:
if(e.Button==MouseButtons.Left)
{
pictureBox1.Image.Dispose();
pictureBox1.Image=\u originalImage.Clone()作为图像;
_选择=真;
_选择=新矩形(新点(e.X,e.Y),新尺寸());
}
}
私有void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
//更新所选内容的实际大小:
如果(_选择)
{
_宽度=(e.X-_selection.X);
_高度=(e.Y-_selection.Y);
//int nGCD=getgreatestcommondivisior(19201080);
//_selection.Width=\u selection.Width/nGCD;
//_selection.Height=\u selection.Height/nGCD;
int宽度比=16;
内部高度比=9;
if(_selection.Height*widthRatio Screen.PrimaryScreen.Bounds.Width)
{
//ActiveForm.Size=新的大小(100100);
Form.ActiveForm.Size=新大小(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
image=image.Fit2PictureBox(pictureBox);
//位图位图=新位图();
}
返回图像;
}
公共静态图像裁剪(此图像,矩形选择)
{
位图bmp=图像作为位图;
尝试
{
//检查是否为位图:
如果(bmp==null)
抛出新ArgumentException(“无有效位图”);
//裁剪图像:
位图cropBmp=bmp.Clone(选择,bmp.PixelFormat);
//释放资源:
image.Dispose();
返回cropBmp;
}
捕获(例外)
{
返回bmp;
}
}
公共静态图像Fit2PictureBox(此图像,PictureBox PictureBox)
{
位图bmp=null;
图形g;
//比例:
双刻度=(双)image.Width/picBox.Width;
double scaleX=(double)image.Height/picBox.Height;
双刻度=scaleY
也许这个例子会有帮助
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CropResize
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args[0]));
}
public static Image SetImageWithinResolution(this Image image, PictureBox pictureBox)
{
//Bitmap bitmap = null;
if (image.Height > Screen.PrimaryScreen.Bounds.Height)
{
//ActiveForm.Size = new Size(100, 100);
Form.ActiveForm.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
image = image.Fit2PictureBox(pictureBox);
//Bitmap bitmap = new Bitmap(image, );
}
if (image.Width > Screen.PrimaryScreen.Bounds.Width)
{
//ActiveForm.Size = new Size(100, 100);
Form.ActiveForm.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
image = image.Fit2PictureBox(pictureBox);
//Bitmap bitmap = new Bitmap();
}
return image;
}
public static Image Crop(this Image image, Rectangle selection)
{
Bitmap bmp = image as Bitmap;
try
{
// Check if it is a bitmap:
if (bmp == null)
throw new ArgumentException("No valid bitmap");
// Crop the image:
Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);
// Release the resources:
image.Dispose();
return cropBmp;
}
catch (Exception)
{
return bmp;
}
}
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( // Destination
0, 0,
bmp.Width, bmp.Height),
new Rectangle( // Source
0, 0,
image.Width, image.Height),
GraphicsUnit.Pixel);
// Release the resources of the graphics:
g.Dispose();
// Release the resources of the origin image:
image.Dispose();
return bmp;
}
}
float ratio = 0.33f;
Rectangle setRect()
{
int x = Math.Min(mDown.X, currPt.X);
int y = Math.Min(mDown.Y, currPt.Y);
int w = Math.Max(mDown.X, currPt.X) - x;
int h = Math.Max(mDown.Y, currPt.Y) - y;
if (ratio > 1) h = (int)(1f * w / ratio);
else w = (int)(1f * h * ratio);
return new Rectangle(x, y, w, h);
}