충격적인 DXT 나 OpenL 등을 사용하지 않고 해상도를 높이면 GetThumbnailImage를 사용하지 않을 것입니다. 많은 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;
}
int int는 무엇입니까? – SLaks
이미지의 품질, jpg가 압축되는 방법에 대해 1에서 100까지 – Landmine
C# Resize 함수가 아닌 C#으로 작성된 GSI + Resize 함수에 대해 묻고 있습니다. –