我对 C# 非常陌生(今天才开始)
我需要能够使用画布处理程序制作 C# Image resizer。例如,一张 500 x 500 的图像需要保持其纵横比。它将被调整为 1024x500。为此,它将保持 500x500,但随后用空白(画布)填充其余空间。
我还将相同的原始图像调整为 300 x 500,其中图像将再次保持其 1:1 比例并调整为 300x300,剩余空间再次用作画布空白。
任何人都可以帮我为此制作一个我能理解的 C# 控制台应用程序吗?
这是我设法找到的我可以合理理解的内容(由于评论很好)。
我发现代码是我可以阅读的,但是当我真正写一些代码时,我就崩溃了。我主要做HTML和CSS。开始扩展到 JQuery 和 C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
namespace ImageResizer
{
class ImageResize
{
static void Main(string[] args)
{
}
public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (OnlyResizeIfWider)
{
if (FullsizeImage.Width <= NewWidth)
{
NewWidth = FullsizeImage.Width;
}
}
int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
NewHeight = MaxHeight;
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(NewFile);
}
}
}