2013-06-03 65 views
-1

我正在尝试调整文件夹中的图像大小。我使用的代码是这样的:使用指定的高度和宽度调整图像大小

string logoUrl = HttpContext.Current.Server.MapPath("DeviceLogo"); 
System.Drawing.Image SourceLogo = System.Drawing.Image.FromFile(logoUrl + @"\" + objDevice.FileName); 

//Create a logo for this device and reseller/client business    
Bitmap newImage = new Bitmap(objDevice.LogoWidth, objDevice.LogoHeight, PixelFormat.Format24bppRgb); 
using (Graphics graphics = Graphics.FromImage(newImage)) 
{ 
    graphics.CompositingQuality = CompositingQuality.HighQuality; 
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    graphics.SmoothingMode = SmoothingMode.HighQuality; 
    graphics.DrawImage(SourceLogo, 0, 0, objDevice.LogoWidth, objDevice.LogoHeight); 
} 
string filepath = HttpContext.Current.Server.MapPath("DeviceLogo");     

//Save the resized image     
newImage.Save(filepath + objDevice.FileName); 

的问题是,该图像是没有得到调整

+0

所以'objDevice.LogoWidth'和'objDevice.LogoHeight'是宽度和高度,图像'SourceLogo'是被调整到?它们与'SourceLogo'的原始尺寸不是相同的尺寸? – DonBoitnott

+0

yes objDevice.LogoWidth和objDevice.LogoHeight是图像的新分辨率 –

回答

0
using(Image img = Image.FromFile(dlgFichier.FileName)) 
{ 
    Image temp = (Image)new Bitmap((Image)img.Clone(), new Size((int)Math.Round(img.Width/ratio), (int)Math.Round(img.Height/ratio))); 
    temp.Save("your path"); 
} 

试试这个

/E在我来说,我想申请一个比保持比例相同的高度和宽度 ,但您可以用您的值替换(int)Math.Round(img.Height/ratio)

/EE取代了我的价值与你

using(Image img = Image.FromFile(dlgFichier.FileName)) 
{ 
    Image temp = (Image)new Bitmap((Image)img.Clone(), new Size(objDevice.LogoWidth, objDevice.LogoHeight)); 
    temp.Save("your path"); 
} 
0
public void ResizeImage(Device objDevice) 
    { 
     string OriginalFile, NewFile; 

     OriginalFile = HttpContext.Current.Server.MapPath("DeviceLogo") + @"\" + objDevice.FileName; 
     NewFile = OriginalFile; 

     System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile); 

     System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(objDevice.LogoWidth, objDevice.LogoHeight, null, IntPtr.Zero); 

     // Clear handle to original file so that we can overwrite it if necessary 
     FullsizeImage.Dispose(); 

     // Save resized picture 
     NewImage.Save(NewFile); 
    } 

由于无帮助

相关问题