调整图像大小并保存 [英] Image resize and Save

查看:78
本文介绍了调整图像大小并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像调整为用户输入的大小并保存.我使用一种方法来做到这一点.但是我不知道如何以正确的方式为我的方法提供参数.我想比这简单的代码

I want to image resize to a user input and want to save it. I use a method to do it. But I don''t know to give parameters to my method correct way. I wanna simple code than this

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
    float nPercent = ((float)Percent / 100);

    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0;
    int sourceY = 0;

    int destX = 0;
    int destY = 0;
    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);

    Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
   grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    grPhoto.Dispose();
    return bmPhoto;
}

推荐答案

我试图清理您发布的代码.您发布代码的最后每一点的绝对灾难是一个警告,我不知道您在做什么,所以我不确定您是否可以理解清楚的答案.我看不到您到底在问什么,哪段代码没有按您期望的那样工作,您想做什么?我不知道您为什么要使用字符串混搭名称,或者您的UI元素为何具有笨拙的名称.但是,我可以告诉您,调整图像大小比您执行的操作要容易得多,这里有一个Bitmap构造函数,它需要一个Bitmap和一个大小,以便可以从中创建新的Bitmap.

编辑您的帖子,以使其显示更少的代码,并说明您希望我们告诉您有关您最终发布的代码的信息,您要执行的操作,不为您执行的操作等.
I have tried to clean up the code you posted. The absolute disaster of you posting every last bit of code is one warning I have that you have no idea what you''re doing, so I''m not sure if you can understand a clear answer. I can''t see exactly what it is you''re asking, which bit of code is not working as you expect, and what do you want to do ? I have no idea why you''re string mashing names, or why your UI elements have idiotic names. However, I can tell you that to resize an image is much easier than what you''re doing, there''s a Bitmap constructor that takes a Bitmap and a size for the new Bitmap to create from it.

Edit your post so it shows even less code, and explains what you want us to tell you about the code you end up posting, and what you''re trying to do, what it''s not doing for you, etc.


有一个简单的方法.
图片src_image = Image.FromFile(path);
图片dst_image = src_image.GetThumbnailImage(height,width,null,System.IntPtr.Zero);
dst_image.Savesavename.bmp,System.Drawing.Imaging.ImageFormat.bmp);
there is an easy method.
Image src_image = Image.FromFile(path);
Image dst_image = src_image.GetThumbnailImage(height, width, null, System.IntPtr.Zero);
dst_image.Savesavename.bmp, System.Drawing.Imaging.ImageFormat.bmp);


这篇关于调整图像大小并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆