带画布处理程序的C#图像大小调整器
我对C#(今天才开始)是个不可思议的新手 我需要能够使用画布处理程序制作一个C#图像大小调整器。例如,500 x 500图像需要保留其纵横比。它的大小将调整为1024x500。要做到这一点,它将保持500x500,但随后用空白(画布)填充空间的其余部分 我还将调整同一原始图像的大小为300 x 500,其中图像将再次保留其1:1比例,并调整为300x300,剩余空间再次用作画布空白 有人能帮我制作一个我能理解的C#控制台应用程序吗 以下是我能够合理理解的内容(由于很好的评论) 我发现我可以阅读代码,但当我实际编写一些代码时,我就崩溃了。我主要做HTML和CSS。开始扩展到JQuery和C#带画布处理程序的C#图像大小调整器,c#,image,canvas,resize,C#,Image,Canvas,Resize,我对C#(今天才开始)是个不可思议的新手 我需要能够使用画布处理程序制作一个C#图像大小调整器。例如,500 x 500图像需要保留其纵横比。它的大小将调整为1024x500。要做到这一点,它将保持500x500,但随后用空白(画布)填充空间的其余部分 我还将调整同一原始图像的大小为300 x 500,其中图像将再次保留其1:1比例,并调整为300x300,剩余空间再次用作画布空白 有人能帮我制作一个我能理解的C#控制台应用程序吗 以下是我能够合理理解的内容(由于很好的评论) 我发现我可以阅读代
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统图;
使用系统、绘图、成像;
名称空间图像大小调整器
{
类ImageResize
{
静态void Main(字符串[]参数)
{
}
public void ResizeImage(string OriginalFile、string NewFile、int NewWidth、int MaxHeight、bool only resizeifwide)
{
System.Drawing.Image FullsizeImage=System.Drawing.Image.FromFile(原始文件);
//防止使用图像内部缩略图
FULLSIZEMAGE.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone);
FULLSIZEMAGE.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone);
如果(仅限ResizeIfWide)
{
if(FullsizeImage.Width最大高度)
{
//改为使用高度调整大小
NewWidth=FullsizeImage.Width*MaxHeight/FullsizeImage.Height;
NewHeight=MaxHeight;
}
System.Drawing.Image NewImage=FullsizeImage.GetThumbnailImage(NewWidth、NewHeight、null、IntPtr.Zero);
//清除原始文件的句柄,以便我们可以在必要时覆盖它
FullsizeImage.Dispose();
//保存调整大小的图片
NewImage.Save(NewFile);
}
}
}
试着去做这个>>
private void resizeImage(string path, string originalFilename,
/* note changed names */
int canvasWidth, int canvasHeight,
/* new */
int originalWidth, int originalHeight)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail =
new Bitmap(canvasWidth, canvasHeight); // changed parm names
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
/* ------------------ new code --------------- */
// Figure out the ratio
double ratioX = (double) canvasWidth / (double) originalWidth;
double ratioY = (double) canvasHeight / (double) originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
// Now calculate the X,Y position of the upper-left corner
// (one of these will always be zero)
int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);
graphic.Clear(Color.White); // white padding
graphic.DrawImage(image, posX, posY, newWidth, newHeight);
/* ------------- end new code ---------------- */
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
private void resizeImage(字符串路径、字符串原始文件名、,
/*注意更改了名称*/
int画布宽度,int画布高度,
/*新的*/
整数原始宽度,整数原始高度)
{
Image=Image.FromFile(路径+原始文件名);
System.Drawing.Image缩略图=
新位图(canvasWidth,canvasHeight);//更改了parm名称
System.Drawing.Graphics图形=
系统。绘图。图形。从图像(缩略图);
graphic.InterpolationMode=InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode=SmoothingMode.HighQuality;
graphic.PixelOffsetMode=PixelOffsetMode.HighQuality;
graphic.CompositingQuality=CompositingQuality.HighQuality;
/*----------------------新代码------------------*/
//算出比率
双比率=(双)画布宽度/(双)原始宽度;
双倍比率=(双倍)画布高度/(双倍)原始高度;
//使用较小的乘数
双比值=比值<比值?比值:比值;
//现在我们可以得到新的高度和宽度
int newHeight=Convert.ToInt32(原始高度*比率);
int newWidth=Convert.ToInt32(原始宽度*比率);
//现在计算左上角的X,Y位置
//(其中一个始终为零)
int posX=Convert.ToInt32((画布宽度-(原始宽度*比率))/2);
int posY=Convert.ToInt32((画布高度-(原始高度*比率))/2);
图形。清晰(颜色。白色);//白色填充
graphic.DrawImage(图像、posX、posY、新宽度、新高度);
/*------------结束新代码------------------*/
System.Drawing.Imaging.ImageCodeInfo[]信息=
ImageCodecInfo.GetImageEncoders();
编码器参数编码器参数;
编码器参数=新的编码器参数(1);
encoderParameters.Param[0]=新的EncoderParameter(Encoder.Quality,
100L);
缩略图.保存(路径+宽度+“+”+原始文件名,信息[1],
编码器参数);
}
试着去做这个>>
private void resizeImage(string path, string originalFilename,
/* note changed names */
int canvasWidth, int canvasHeight,
/* new */
int originalWidth, int originalHeight)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail =
new Bitmap(canvasWidth, canvasHeight); // changed parm names
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
/* ------------------ new code --------------- */
// Figure out the ratio
double ratioX = (double) canvasWidth / (double) originalWidth;
double ratioY = (double) canvasHeight / (double) originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
// Now calculate the X,Y position of the upper-left corner
// (one of these will always be zero)
int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);
graphic.Clear(Color.White); // white padding
graphic.DrawImage(image, posX, posY, newWidth, newHeight);
/* ------------- end new code ---------------- */
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
private void resizeImage(字符串路径、字符串原始文件名、,
/*注意更改了名称*/
int画布宽度,int画布高度,
/*新的*/
整数原始宽度,整数原始高度)
{
Image=Image.FromFile(路径+原始文件名);
System.Drawing.Image缩略图=
新位图(canvasWidth,canvasHeight);//更改了parm名称
System.Drawing.Graphics图形=
系统。绘图。图形。从图像(缩略图);
graphic.InterpolationMode=InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode=SmoothingMode.HighQuality;
graphic.PixelOffsetMode=PixelOffsetMode.HighQuality;
graphic.CompositingQuality=CompositingQuality.HighQuality;
/*----------------------新代码------------------*/
//算出比率
双比率=(双)画布宽度/(双)原始宽度;
双倍比率=(双倍)画布高度/(双倍)原始高度;
//使用较小的乘数
双比值=比值<比值?比值:比值;
//现在我们可以得到新的高度和宽度
int newHeight=Convert.ToInt32(原始高度*比率);
int newWidth=Convert.ToInt32(原始宽度*比率);
//现在计算左上角的X,Y位置
//(其中一个始终为零)
int posX=Convert.ToInt32((画布宽度-(原始宽度*比率))/2);
int posY=Convert.ToInt32((canvasHeig