Forum > ASP.NET 3.x > Språk - C#
Hejsan,
jag försöker skala om en jpeg bild i ASP.NET, men det förlorar ganska mycket kvalitet...
System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath + @"\" + sOrgFileName);
System.Drawing.Image oThumbNail = new Bitmap(this.ThumbNailSize.Width,
this.ThumbNailSize.Height, oImg.PixelFormat);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
Rectangle oRectangle = new Rectangle(0, 0, this.ThumbNailSize.Width, this.ThumbNailSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
oThumbNail.Save(sPhysicalPath + @"\" + sThumbNailFileName,oFormat);
oImg.Dispose();
Om du enbart ska skala om bilden till ett visst antal pixlar så räcker det gott och väl att göra bilden till DrawingImage och sedan skapa en Bitmap med den width och height du vill ha.
Något liknande detta:
protected void ScaleImage(string sourceImage, int width, int height) {
System.Drawing.Image image = System.Drawing.Image.FromFilesourceIimage);
Bitmap bitmap = new Bitmap(image, width, height);
bitmap.Save(Server.MapPath("Image.jpg"), image.RawFormat);
}