![](/img/trans.png)
[英]How to get image width and height uploaded using ajaxfileupload asp.net
[英]ASP.NET: resize (height and width) an image uploaded to server
如何調整剛上傳的服務器上圖像的大小? 我將C#與.NET Framework 3.5 SP1結合使用。
謝謝!
請嘗試以下方法:
public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
{
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
double widthRatio = (double)fullSizeImg.Width / (double)Width;
double heightRatio = (double)fullSizeImg.Height / (double)Height;
double ratio = Math.Max(widthRatio, heightRatio);
int newWidth = (int)(fullSizeImg.Width / ratio);
int newHeight = (int)(fullSizeImg.Height / ratio);
//System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
//DateTime MyDate = DateTime.Now;
//String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
thumbNailImg.Save(destPath, ImageFormat.Jpeg);
thumbNailImg.Dispose();
return "";
}
public bool ThumbnailCallback() { return false; }
你有嘗試過嗎?
public Image resize( Image img, int width, int height )
{
Bitmap b = new Bitmap( width, height ) ;
Graphics g = Graphics.FromImage( (Image ) b ) ;
g.DrawImage( img, 0, 0, width, height ) ;
g.Dispose() ;
return (Image ) b ;
}
我經常使用的代碼段:
var target = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
target.SetResolution(source.HorizontalResolution,
source.VerticalResolution);
using (var graphics = Graphics.FromImage(target))
{
graphics.Clear(Color.White);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(source,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, source.Width, source.Height),
GraphicsUnit.Pixel);
}
返回目標;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.