내 원래의 대답은 파일 크기가 인플레이션이었던 것과 동일한 문제가 없다고 가정하고이 코드를 게시했습니다 ... 잘못된 것입니다. 그래서 약간의 조작을 한 후에 JPG 이미지가 JPG 확장자로 저장된다는 것을 알았지 만 PNG로 인코딩되므로 파일 크기가 커졌습니다. 다음은 PNG, GIF 및 JPG를 사용하여 신뢰할 수있는 테스트를 거친 업데이트 된 코드베이스입니다. 이미지 크기가 원본보다 작 으면 파일 크기가 작아집니다.
처음에는 비트 맵을 가져 와서 크기를 조정하는 기본 방법입니다.
public static Bitmap Resize(Bitmap imgPhoto, Size objSize, ImageFormat enuType)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = objSize.Width;
int destHeight = objSize.Height;
Bitmap bmPhoto;
if (enuType == ImageFormat.Png)
bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format32bppArgb);
else if (enuType == ImageFormat.Gif)
bmPhoto = new Bitmap(destWidth, destHeight); //PixelFormat.Format8bppIndexed should be the right value for a GIF, but will throw an error with some GIF images so it's not safe to specify.
else
bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
//For some reason the resolution properties will be 96, even when the source image is different, so this matching does not appear to be reliable.
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//If you want to override the default 96dpi resolution do it here
//bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
여기 크기 조정 방법을 사용하는 방법은 ...이 방법은 복잡한 계층을 추가
String strImageFile = Server.MapPath("/Images/ImageFile.jpg");
System.Drawing.Bitmap objImage = new System.Drawing.Bitmap(strImageFile);
System.Drawing.Size objNewSize = new System.Drawing.Size(100, 50);
System.Drawing.Bitmap objNewImage = Resize(objImage, objNewSize, ImageFormat.Jpeg);
objNewImage.Save(Server.MapPath("/Images/FileName_Resized.jpg"), ImageFormat.Jpeg);
objNewImage.Dispose();
, 당신은 최대 크기의 제약 조건을 정의 할 수 있습니다 및 방법에 머물 이미지 크기를 조정합니다 비율 ...하지만 최대 크기보다 클 수 없습니다. 이미지가 최대 크기보다 작거나 같으면 이미지를 그대로두고 원본 비트 맵을 반환합니다.
public static Bitmap SmartResize(string strImageFile, Size objMaxSize, ImageFormat enuType)
{
Bitmap objImage = null;
try
{
objImage = new Bitmap(strImageFile);
}
catch (Exception ex)
{
throw ex;
}
if (objImage.Width > objMaxSize.Width || objImage.Height > objMaxSize.Height)
{
Size objSize;
int intWidthOverrun = 0;
int intHeightOverrun = 0;
if (objImage.Width > objMaxSize.Width)
intWidthOverrun = objImage.Width - objMaxSize.Width;
if (objImage.Height > objMaxSize.Height)
intHeightOverrun = objImage.Height - objMaxSize.Height;
double dblRatio;
double dblWidthRatio = (double)objMaxSize.Width/(double)objImage.Width;
double dblHeightRatio = (double)objMaxSize.Height/(double)objImage.Height;
if (dblWidthRatio < dblHeightRatio)
dblRatio = dblWidthRatio;
else
dblRatio = dblHeightRatio;
objSize = new Size((int)((double)objImage.Width * dblRatio), (int)((double)objImage.Height * dblRatio));
Bitmap objNewImage = Resize(objImage, objSize, enuType);
objImage.Dispose();
return objNewImage;
}
else
{
return objImage;
}
}
여기 ... 그것을 구현하는 방법 나는 당신의 모든 방법을 테스트하고 있습니다
String strImageFile = Server.MapPath("/Images/ImageFile.png");
System.Drawing.Size objMaxSize = new System.Drawing.Size(100, 100);
System.Drawing.Bitmap objNewImage = SmartResize(strImageFile, objMaxSize, ImageFormat.Png);
objNewImage.Save(Server.MapPath("/Images/FileName_Resized.png"), ImageFormat.Png);
objNewImage.Dispose();
이다하지만 난 내 코드를 withing에 함수를 호출 할 수 없습니다. 이것은 ResizeBitmap 함수를 호출하는 원래 코드입니다. 나는 그것을 당신의 방법이라고 부르기 위해 바꿨지만 효과가 없었다. 당신은 당신의 방법과 어떤 주장을 어떻게 부릅니까? System.Drawing.Image bm = System.Drawing.Image.FromStream (imageFile.InputStream); bm = ResizeBitmap (비트 맵) bm, 1600); /// 새로운 너비, 높이 – Gloria
좋아, 이제 정답을 얻었습니다. 내 코드가 파일 크기를 너무 팽창 시켰습니다. PNG 인코딩을 사용하여 ".jpg"파일을 저장했기 때문에이 파일을 찾았습니다. 업데이트 된 대답은이 문제를 해결하고 PNG, JPG 및 GIF 이미지로 안정적인 테스트를 거쳐 크기를 조정하고 크기를 줄입니다. –