c# - 剪切图像的中心

标签 c# image image-processing

假设我有一个 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/

相关文章:

c# - 在 C# 中不使用 GDI+ 处理图像

c# - 取决于另一个项目的 MSBuild 自定义任务

c# - 将用户重定向到 asp.net 中的同一动态页面。

html - 如何使图像悬停在另一个图像上?

Javascript 加载图片

c - 如何用Matlab或C控制鼠标指针?

c++ - haar 级联训练时出现 bad_alloc() 错误

c# - 使用标题和行列表创建 View

c# - 如何最好地限制通用 <T> 类中的类型,其中所有类型都派生自同一基类

image - HSI 和 YCbCr 之间的差异与肤色相关