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

C# 使用指定的高度和宽度调整图像大小,c#,C#,我正在尝试调整文件夹中图像的大小。我使用的代码如下: 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 b

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

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");
}
试试这个

/在我的例子中,我想应用一个比例,使比例保持相同的高度和宽度 但是您可以用您的值替换(int)Math.Round(img.Height/ratio)

/你的价值取代了我的价值

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

感谢None的帮助

因此
objDevice.LogoWidth
objDevice.LogoHeight
是图像
SourceLogo
要调整大小的宽度和高度吗?它们的尺寸与
SourceLogo
的原始尺寸不同?是的,objDevice.logowith和objDevice.logowith是图像的新分辨率
    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);
    }