所以我的邏輯是有缺陷的,我需要一個更好的和正確的方式在我的C#應用程序來調整圖像C#GDI +圖片縮放功能
我需要類似於此設置函數
public void ResizeImageForWeb(string OriginalFile, string NewFile, int MaxWidth, int MaxHeight, int Quality)
{
// Resize Code
}
基本上,我是一名網頁設計師,無法嘗試編寫桌面應用程序。
所以我的邏輯是有缺陷的,我需要一個更好的和正確的方式在我的C#應用程序來調整圖像C#GDI +圖片縮放功能
我需要類似於此設置函數
public void ResizeImageForWeb(string OriginalFile, string NewFile, int MaxWidth, int MaxHeight, int Quality)
{
// Resize Code
}
基本上,我是一名網頁設計師,無法嘗試編寫桌面應用程序。
這是我用來調整圖像大小的代碼,用戶上傳的圖像可能會創建縮略圖或者只是爲了強制執行大小限制。它不涉及圖像質量,但它是一個開始。
// uses System.Drawing namespace
public class ImageResizer
{
public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth)
{
return this.ResizeImage(fullFileName, maxHeight, maxWidth, fullFileName);
}
public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth, string newFileName)
{
using (Image originalImage = Image.FromFile(fullFileName))
{
int height = originalImage.Height;
int width = originalImage.Width;
int newHeight = maxHeight;
int newWidth = maxWidth;
if (height > maxHeight || width > maxWidth)
{
if (height > maxHeight)
{
newHeight = maxHeight;
float temp = ((float)width/(float)height) * (float)maxHeight;
newWidth = Convert.ToInt32(temp);
height = newHeight;
width = newWidth;
}
if (width > maxWidth)
{
newWidth = maxWidth;
float temp = ((float)height/(float)width) * (float)maxWidth;
newHeight = Convert.ToInt32(temp);
}
Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback);
using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero))
{
resizedImage.Save(newFileName);
}
return true;
}
else if (fullFileName != newFileName)
{
// no resizing necessary, but need to create new file
originalImage.Save(newFileName);
}
}
return false;
}
private bool ThumbnailCallback()
{
return false;
}
}
當它只有800像素寬,任何方式來縮小一個1.8 MB的圖像爲1MB的圖像文件的大小? – Landmine 2010-05-18 23:17:18
也許看看其他重載的Save方法 http://msdn.microsoft.com/en-us/library/8ex6sdew(v=VS.100).aspx http://msdn.microsoft.com/en-us /library/ytz20d80(v=VS.100).aspx – 2010-05-18 23:28:30
圖片質量很糟糕!調整大小很好,但不能使用這個。 – Landmine 2010-05-19 00:10:21
使用Graphics.DrawImage()。 GetThumbnailImage()將從jpeg文件返回一個120x120(或更小)的嵌入縮略圖。對於超過這個尺寸的任何東西將是可怕的。
請參閱http://nathanaeljones.com/163/20-image-resizing-pitfalls/以瞭解適用的設置。
我肯定不會使用GetThumbnailImage,因爲它會令人震驚 - 對於不使用DX或OpenL等的良好分辨率,我會使用類似下面的內容(從我自己的圖形庫中,我用在許多Windows應用程序 - 我有之前共享過幾次,所以可能會有變體浮在網上)。這裏有3個方法 - GetNonIndexedPixelFormat方法用於在傳遞的像素格式無法處理時阻止GDI崩潰(註釋解釋它)。第一個允許縮放一個因子(縮放),最後一個允許固定尺寸縮放,同時保持縱橫比(但如果您想改變它的話,可以很容易地修改)。享受:
/// <summary>
/// Scale Image By A Percentage - Scale Factor between 0 and 1.
/// </summary>
/// <param name="originalImg">Image: Image to scale</param>
/// <param name="ZoomFactor">Float: Sclae Value - 0 to 1 are the usual values</param>
/// <returns>Image: Scaled Image</returns>
public static Image ScaleByPercent(Image originalImg, float ZoomFactor)
{
int destWidth = (int)((float)originalImg.Width * ZoomFactor);
int destHeight = (int)((float)originalImg.Height * ZoomFactor);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, GetNonIndexedPixelFormat(originalImg)); // PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(originalImg,
new Rectangle(0, 0, destWidth, destHeight),
new Rectangle(0, 0, originalImg.Width, originalImg.Height),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
/// <summary>
/// Gets the closest non-indexed pixel format
/// </summary>
/// <param name="originalImage">Image: Original image</param>
/// <returns>PixelFormat: Closest non-pixel image format</returns>
public static PixelFormat GetNonIndexedPixelFormat(Image originalImage)
{
/*
* These formats cause an error when creating a GDI Graphics Oblect, so must be converted to non Indexed
* Error is "A graphics object cannot be created from an image that has an indexed pixel format"
*
PixelFormat.Undefined
PixelFormat.DontCare
PixelFormat.1bppIndexed
PixelFormat.4bppIndexed
PixelFormat.8bppIndexed
PixelFormat.16bppGrayScale
PixelFormat.16bppARGB1555
*
* An attempt is made to use the closest (i.e. smallest fitting) format that will hold the palette.
*/
switch (originalImage.PixelFormat)
{
case PixelFormat.Undefined:
//This is also the same Enumation as PixelFormat.DontCare:
return PixelFormat.Format24bppRgb;
case PixelFormat.Format1bppIndexed:
return PixelFormat.Format16bppRgb555;
case PixelFormat.Format4bppIndexed:
return PixelFormat.Format16bppRgb555;
case PixelFormat.Format8bppIndexed:
return PixelFormat.Format16bppRgb555;
case PixelFormat.Format16bppGrayScale:
return PixelFormat.Format16bppArgb1555;
case PixelFormat.Format32bppArgb:
return PixelFormat.Format24bppRgb;
default:
return originalImage.PixelFormat;
}
}
/// <summary>
/// Resize image keeping aspect ratio.
/// </summary>
/// <param name="originalImg">Image: Image to scale</param>
/// <param name="Width">Int: Required width in pixels</param>
/// <param name="Height">Int: Required height in pixels</param>
/// <param name="BackgroundColour">Color: Background colour</param>
/// <returns>Image: Scaled Image</returns>
public static Image Resize(Image originalImg, int Width, int Height, Color BackgroundColour)
{
int destX = 0;
int destY = 0;
float nPercent = 0f;
float nPercentW = ((float)Width/(float)originalImg.Width);
float nPercentH = ((float)Height/(float)originalImg.Height);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16(((float)Width - ((float)originalImg.Width * nPercent))/2f);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16(((float)Height - ((float)originalImg.Height * nPercent))/2f);
}
int destWidth = (int)(originalImg.Width * nPercent);
int destHeight = (int)(originalImg.Height * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, GetNonIndexedPixelFormat(originalImg)); // PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(BackgroundColour);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(originalImg,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(0, 0, originalImg.Width, originalImg.Height), GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
+1用於報告此處的代碼,而不鏈接到可能隨時間過期的外部資源。事實上,接受的答案中的鏈接不再起作用。 謝謝。 – vaitrafra 2016-09-24 13:40:22
什麼是'int Quality'? – SLaks 2010-05-18 23:10:29
圖像質量,1到100如何壓縮jpg – Landmine 2010-05-18 23:15:39
請注意,您問的是使用C#編寫的GDI +調整大小函數,而不是C#調整大小函數。 – 2010-05-18 23:29:08