假设我有一个 400x300px 的图像,我想在服务器端(C#、.NET 4.0)将它剪切为 200x200px 居中。
我该怎么做?使用一种 Canvas 并移动它?任何教程/代码示例/建议?
最佳答案
尝试这样的事情:
Bitmap sourceImage = ...;
int targetWidth = 200;
int targetHeight = 200;
int x = sourceImage.Width / 2 - targetWidth / 2;
int y = sourceImage.Height / 2 - targetHeight / 2;
Rectangle cropArea =
new Rectangle(x, y, targetWidth, targetHeight);
Bitmap targetImage =
sourceImage.Clone(cropArea, sourceImage.PixelFormat);
如果源图像小于目标图像大小,这显然会失败,但你明白了。
关于c# - 剪切图像的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242762/