【发布时间】:2017-05-10 19:36:17
【问题描述】:
我有通常小于 500x500 像素的位图图像。 但有时一个或两个维度可能超过 500 像素。 如果高度> 500,我想在底部裁剪图像,如果宽度> 500,我想在两侧均等地裁剪它。 如果图像在任何维度上小于 500,我想在每一侧用白色像素填充它,使其成为 500x500。 我不熟悉 .NET,但我知道已经为您完成了很多工作(我是 C++ 开发人员)。 我很感激任何帮助!谢谢!
这是我目前所拥有的,它将图像置于白色 500x500 矩形图像的中心。只是对于原始图像的一维超过 500 像素的情况,我无法理解。 (见??这两行)
public static System.Drawing.Bitmap PadImage(System.Drawing.Bitmap originalImage)
{
if (originalImage.Height > 500)
??
if (originalImage.Width > 500)
??
Size squareSize = new Size(500, 500);
System.Drawing.Bitmap squareImage = new System.Drawing.Bitmap(squareSize.Width, squareSize.Height);
using (Graphics graphics = Graphics.FromImage(squareImage))
{
graphics.FillRectangle(System.Drawing.Brushes.White, 0, 0, squareSize.Width, squareSize.Height);
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.DrawImage(originalImage, (squareSize.Width / 2) - (originalImage.Width / 2), (squareSize.Height / 2) - (originalImage.Height / 2), originalImage.Width, originalImage.Height);
}
return squareImage;
}
【问题讨论】:
-
这表明没有任何研究工作
-
你问了 5 个问题......
-
@EpicKip 抱歉,我发的太快了,没有加代码。
-
假设它是 1000*1000,你会剪切它还是调整它的大小?因为我刚刚意识到我面前的东西几乎就是答案