使用指定的高度和宽度调整图像大小

本文关键字:调整 图像 高度 | 更新日期: 2023-09-27 18:16:46

我正在尝试调整文件夹中图像的大小。我使用的代码是:

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);

问题是图像没有被调整大小

使用指定的高度和宽度调整图像大小

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。四舍五入(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");
}
    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);
    }

感谢None帮助