回答
您正在尋找縮放圖像,並保留寬高比:
float MaxRatio = MaxWidth/(float) MaxHeight;
float ImgRatio = source.Width/(float) source.Height;
if (source.Width > MaxWidth)
return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth/
ImgRatio, 0)));
if (source.Height > MaxHeight)
return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio,
0), MaxHeight));
return source;
應該幫助你,如果你有興趣的想法:Wikpedia article on Image Aspect Ratio
剛剛嘗試過這段代碼(在搜索相同的基本問題時發現了其他地方),如果從寬度和高度均大於最大值的圖像開始,它實際上不會完成這項工作 - 您需要根據原始圖像的哪個尺寸最大來應用第一或第二縮放比例,否則最終尺寸中的一個大於任一縮放中允許的最大尺寸。 – Moo 2010-01-15 20:46:36
看看Bertrands blog post關於使用GDI和WPF縮放圖像。
需要保留寬高比:
float scale = 0.0;
if (newWidth > maxWidth || newHeight > maxHeight)
{
if (maxWidth/newWidth < maxHeight/newHeight)
{
scale = maxWidth/newWidth;
}
else
{
scale = maxHeight/newHeight;
}
newWidth = newWidth*scale;
newHeight = newHeight*scale;
}
在代碼中,最初newWidth/newHeight是圖像的寬度/高度。
如果newHeight或newWidth是一個整數,它將無法正常工作,您將需要一個強制轉換爲'float'。 – BrunoLM 2010-11-04 10:44:13
基於的Brij的答案我做了這個擴展方法:
/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxWidth">Max width</param>
/// <param name="maxHeight">Max height</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, int maxWidth, int maxHeight)
{
double scale = 1;
if (img.Width > maxWidth || img.Height > maxHeight)
{
double scaleW, scaleH;
scaleW = maxWidth/(double)img.Width;
scaleH = maxHeight/(double)img.Height;
scale = scaleW < scaleH ? scaleW : scaleH;
}
return img.Resize((int)(img.Width * scale), (int)(img.Height * scale));
}
/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxDimensions">Max image size</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, Size maxDimensions)
{
return img.Scale(maxDimensions.Width, maxDimensions.Height);
}
容量調整方法:
/// <summary>
/// Resize the image to the given Size
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="width">Width size</param>
/// <param name="height">Height size</param>
/// <returns>Resized Image</returns>
public static Image Resize(this Image img, int width, int height)
{
return img.GetThumbnailImage(width, height, null, IntPtr.Zero);
}
而不是img.Height,img.Source.Height爲我工作(VS 2010 .net 4)。另外,Source被用於Width – mnemonic 2013-01-23 19:47:58
- 1. 保持像素比例,但縮放尺寸
- 2. 在UIScrollView中旋轉圖像時保持縮放比例
- 3. 滾動時縮放圖像(保持良好比例)
- 4. 縮放比例按鈕保持背景圖像
- 5. 縮放任何圖像保持比例和最小寬度
- 6. 比例圖像縮放
- 7. 在Android上保持畫布比例和縮放比例
- 8. 調整HTML圖像的縮放比例
- 9. 表中圖像的比例縮放
- 10. Window.open圖像和縮放比例
- 11. 如何停止比例圖像縮放?
- 12. C中的圖像縮放比例
- 13. AS3按比例縮放外部圖像
- 14. Flexbox圖像縮放到高度並保持縱橫比
- 15. 縮放圖像保持背景縱橫比可繪製
- 16. 背景圖像縮放,同時保持縱橫比
- 17. 調整圖像大小保持比例
- 18. rmagick調整圖像保持比例
- 19. 如何調整圖像大小,但保持與JavaScript的比例?
- 20. Python /枕頭:縮放兒童圖像,但保持父圖像的尺寸
- 21. 如何縮放佈局雖然保持其兒童原始比例
- 22. Fabricjs objectCaching:縮放圖像內容,保持圖像邊框
- 23. 我應該如何在PinchGestureRecognizer上保持縮放比例?
- 24. TransformGestureEvent縮放圖像,但保留圖像大小Flex
- 25. CENTER_CROP不保持圖像比
- 26. Android WebView使用setWideViewPort,禁用雙擊縮放但保持縮放?
- 27. 刷新縮放圖像,同時保持它放大android
- 28. 圖像邊框保持120x120,而圖像保持長寬比
- 29. 縮放圖像,以適應容器,同時保持長寬比與jquery
- 30. Qt - 如何創建窗口縮放圖像並保持寬高比?
這似乎類似於http://stackoverflow.com/questions/2823200/。看到我對這個問題的回答。 – 2010-11-04 03:26:45