6
A
回答
8
縮放圖像到一個PictureBox:
class ImageHandling
{
public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect)
{
if (img.Width < thumbRect.Width && img.Height < thumbRect.Height)
return new Rectangle(thumbRect.X + ((thumbRect.Width - img.Width)/2), thumbRect.Y + ((thumbRect.Height - img.Height)/2), img.Width, img.Height);
int sourceWidth = img.Width;
int sourceHeight = img.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)thumbRect.Width/(float)sourceWidth);
nPercentH = ((float)thumbRect.Height/(float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
if (destWidth.Equals(0))
destWidth = 1;
if (destHeight.Equals(0))
destHeight = 1;
Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);
if (retRect.Height < thumbRect.Height)
retRect.Y = retRect.Y + Convert.ToInt32(((float)thumbRect.Height - (float)retRect.Height)/(float)2);
if (retRect.Width < thumbRect.Width)
retRect.X = retRect.X + Convert.ToInt32(((float)thumbRect.Width - (float)retRect.Width)/(float)2);
return retRect;
}
public static Image GetResizedImage(Image img, Rectangle rect)
{
Bitmap b = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, rect.Width, rect.Height);
g.Dispose();
try
{
return (Image)b.Clone();
}
finally
{
b.Dispose();
b = null;
g = null;
}
}
}
以及如何使用它:
Image img = Image.FromFile(imageFilePath);
Rectangle newRect = GetScaledRectangle(img, pictureBox1.ClientRectangle);
pictureBox1.Image = ImageHandling.GetResizedImage(img, newRect);
0
Sacle和中心的ImageBox:
private void LoadAndResizeImage(string sImagepath, int iMaxHeight, int iMaxWidth, int iMinX, int iMinY)
{
int wdt = iMaxWidth;
int hgt = iMaxHeight;
int partialX = iMinX;
int partialY = iMinY;
pictureBox1.ImageLocation = sImagepath;
if ((pictureBox1.Image.Height > iMaxHeight) || (pictureBox1.Image.Width > iMaxWidth))
{
if ((pictureBox1.Image.Width/iMaxWidth) > (pictureBox1.Image.Height/iMaxHeight))
{
wdt = iMaxWidth;
double ratio = (double)pictureBox1.Image.Height/(double)pictureBox1.Image.Width;
hgt = (int)(iMaxWidth * ratio);
}
else
{
hgt = iMaxHeight;
double ratio = (double)pictureBox1.Image.Width/(double)pictureBox1.Image.Height;
wdt = (int)(iMaxHeight * ratio);
}
}
else
{
hgt = pictureBox1.Image.Height;
wdt = pictureBox1.Image.Width;
}
if (wdt < iMaxWidth)
{
partialX = (iMaxWidth - wdt)/2;
partialX += iMinX;
}
else
{
partialX = iMinX;
}
if (hgt < iMaxHeight)
{
partialY = (iMaxHeight - hgt)/2;
partialY += iMinY;
}
else
{
partialY = iMinY;
}
//Set size
pictureBox1.Height = hgt;
pictureBox1.Width = wdt;
pictureBox1.Left = partialX;
pictureBox1.Top = partialY;
}
相關問題
- 1. 控制調整大小時調整圖像的大小c#
- 2. C#調整webclient的圖像大小
- 3. 調整大小TIFF圖像使用C#
- 4. 調整圖像大小Objective-C
- 5. 調整大小在C#單色圖像
- 6. C#圖像自動調整大小
- 7. 以釐米C調整圖像大小#
- 8. 調整圖像大小
- 9. Drupal - 圖像調整大小
- 10. 圖像的大小調整
- 11. 調整圖像大小5
- 12. php調整大小圖像
- 13. 調整圖像大小
- 14. Opencart - 圖像調整大小
- 15. JavaME調整圖像大小
- 16. PHP:調整圖像大小
- 17. Javascript圖像調整大小
- 18. 圖像調整大小捏
- 19. 調整圖像大小 - Symfony
- 20. TCPDF圖像調整大小
- 21. imageWithContentsOfFile調整圖像大小
- 22. CGRect圖像調整大小
- 23. CSS圖像大小調整
- 24. JavaFx圖像調整大小
- 25. iphone圖像大小調整
- 26. Android圖像調整大小
- 27. ckfinder圖像調整大小
- 28. Kraken.io圖像調整大小
- 29. jquery調整大小圖像
- 30. 調整圖像大小
我很抱歉,但什麼這個方法解決了嗎?除了與Image類緊密耦合之外,這不會調整圖片的大小,只是提供了一種計算在調整到定義的寬度和高度(tumbRect)時如何保持寬高比的方法。 – 2010-02-03 10:27:21
我正在使用它來正確縮放在固定大小的圖片框上顯示的圖片,而不用接觸原始文件。 – Amirshk 2010-02-03 10:35:26
好吧,實際調整大小發生在'ImageHandler.GetResizeImage'中,這是我自己假定的一個實用類? – 2010-02-03 10:48:24