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. 如何製作方形圖?
- 2. 在圖形中製作圖像鏈接 - 圖像大小變化
- 3. 縮小比例並在android中製作圖像正方形
- 4. 如何將現有圖像製作成回形針圖像?
- 5. 圓形圖像按鈕VS方形控制器
- 6. 繪製矩形圖像 - LibGdx
- 7. 繪製圖像的形狀
- 8. 使用背景圖像製作菱形形狀
- 9. 使用Laravel 5干涉圖像爲圖像添加空白區域以製作方形圖像
- 10. RecyclerView加載方形圖像
- 11. 將矩形圖像繪製/翹曲爲四邊形圖像
- 12. 將方形圖像轉換爲矩形
- 13. 如何製作圓形方形組圖標而不是圓形圖標
- 14. 使用圖像作爲節點的D3 v4強制圖形
- 15. 讓回形針製作矢量圖像的柵格縮略圖
- 16. 帶圖形C的圖像繪製#
- 17. 用xCode製作圖形
- 18. PIL:縮圖和具有方形圖像
- 19. 爲什麼繪製OnPaint圖形比圖像圖形更快?
- 20. 將圖像操作應用於圖像以製作新圖像
- 21. 製作4個方塊的正方形
- 22. 如何在matplotlib中使用熱圖製作方形子圖?
- 23. 不使用「紋理矩形」方法繪製圖像
- 24. 強制圖像爲正方形並以css居中
- 25. 定製VisJS與圖像和文字的方形標籤
- 26. 在用戶指定的圖像上繪製圓圈/正方形
- 27. 無法在XNA中繪製超過正方形的圖像
- 28. 如何製作用不同顏色覆蓋的圓形圖像
- 29. 根據屏幕大小使用css製作圓形圖像
- 30. 如何在css中製作圓形圖像
你想新重新採樣的圖像放入一個正方形白色帆布,使得圖像保留其長寬比或拉伸,以適應方形(使白色帆布那種不必要的)? – JYelton 2010-05-12 23:35:31
我想保留長寬比 - 沒有拉伸 – 2010-05-13 00:15:12
@ MichaelD.aren't那些不相容?平方改變寬高比。或者是裁剪圖像可以接受? – kenny 2015-06-29 21:00:33