Q
制作方形图像
11
A
回答
21
这实际上可以很容易地完成。
public static Image PadImage(Image originalImage)
{
int largestDimension = Math.Max(originalImage.Height, originalImage.Width);
Size squareSize = new Size(largestDimension, largestDimension);
Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
using (Graphics graphics = Graphics.FromImage(squareImage))
{
graphics.FillRectangle(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;
}
+0
我知道这是一个旧帖子,但它仍然有用。我实际上使用Math.Min裁剪我的图像来获取缩略图。完美的作品! – AntLaC 2017-08-27 11:55:30
2
请尝试使用此方法。最后一个参数是一个开关,用于确定是否要拉伸图像以适合。如果为false,则图像将位于新白色画布的中央。您可以根据需要将正方形或非正方形大小传递给它。
public static Bitmap ResizeBitmapOnWhiteCanvas(Bitmap bmpOriginal, Size szTarget, bool Stretch)
{
Bitmap result = new Bitmap(szTarget.Width, szTarget.Height);
using (Graphics g = Graphics.FromImage((Image)result))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.FillRectangle(Brushes.White, new Rectangle(0, 0, szTarget.Width, szTarget.Height));
if (Stretch)
{
g.DrawImage(bmpOriginal, 0, 0, szTarget.Width, szTarget.Height); // fills the square (stretch)
}
else
{
float OriginalAR = bmpOriginal.Width/bmpOriginal.Height;
float TargetAR = szTarget.Width/szTarget.Height;
if (OriginalAR >= TargetAR)
{
// Original is wider than target
float X = 0F;
float Y = ((float)szTarget.Height/2F) - ((float)szTarget.Width/(float)bmpOriginal.Width * (float)bmpOriginal.Height)/2F;
float Width = szTarget.Width;
float Height = (float)szTarget.Width/(float)bmpOriginal.Width * (float)bmpOriginal.Height;
g.DrawImage(bmpOriginal, X, Y, Width, Height);
}
else
{
// Original is narrower than target
float X = ((float)szTarget.Width/2F) - ((float)szTarget.Height/(float)bmpOriginal.Height * (float)bmpOriginal.Width)/2F;
float Y = 0F;
float Width = (float)szTarget.Height/(float)bmpOriginal.Height * (float)bmpOriginal.Width;
float Height = szTarget.Height;
g.DrawImage(bmpOriginal, X, Y, Width, Height);
}
}
}
return result;
}
1
你不说你想如何填充。假设你想要的形象为中心,在映像文件名称的图像文件名和所需的输出文件名newFileName:
Bitmap orig = new Bitmap(imageFileName);
int dim = Math.Max(orig.Width, orig.Height);
Bitmap dest;
using (Graphics origG = Graphics.FromImage(orig))
{
dest = new Bitmap(dim, dim, origG);
}
using (Graphics g = Graphics.FromImage(dest))
{
Pen white = new Pen(Color.White, 22);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, dim, dim);
g.DrawImage(orig, new Point((dim - orig.Width)/2, (dim - orig.Height)/2));
}
dest.Save(newFileName);
相关问题
- 1. 在Volley中制作方形图像.NetworkingImageView
- 2. 如何制作方形图?
- 3. 如何在Windows Phone上对圆形图像制作方形图像
- 4. 如何制作圆形图像
- 5. 在图形中制作图像链接 - 图像大小变化
- 6. 缩小比例并在android中制作图像正方形
- 7. 如何将现有图像制作成回形针图像?
- 8. 绘制图形直方图
- 9. 圆形图像按钮VS方形控制器
- 10. 绘制矩形图像 - LibGdx
- 11. 将图像制成圆形
- 12. 绘制图像的形状
- 13. Android图形制作线条条形图
- 14. 使用背景图像制作菱形形状
- 15. 使用Laravel 5干涉图像为图像添加空白区域以制作方形图像
- 16. RecyclerView加载方形图像
- 17. 将矩形图像绘制/翘曲为四边形图像
- 18. 将方形图像转换为矩形
- 19. 将矩形图像重新调整为方形图像
- 20. PHP - 如何将矩形图像转换为方形图像?
- 21. 如何制作圆形方形组图标而不是圆形图标
- 22. 像开放图形动作
- 23. 作物圆形图像
- 24. 制作一个正方形
- 25. 无法在图形类中使用drawImage方法绘制图像
- 26. 让回形针制作矢量图像的栅格缩略图
- 27. 使用图像作为节点的D3 v4强制图形
- 28. 带图形C的图像绘制#
- 29. 绘制图像用C#图形
- 30. 改变内插RGB图像的图像宽高比。正方形到长方形
你想新重新采样的图像放入一个正方形白色帆布,使得图像保留其长宽比或拉伸,以适应方形(使白色帆布那种不必要的)? – JYelton 2010-05-12 23:35:31
我想保留长宽比 - 没有拉伸 – 2010-05-13 00:15:12
@ MichaelD.aren't那些不相容?平方改变宽高比。或者是裁剪图像可以接受? – kenny 2015-06-29 21:00:33