C# 在c中从memorystream保存为jpeg#

C# 在c中从memorystream保存为jpeg#,c#,image,jpeg,memorystream,C#,Image,Jpeg,Memorystream,我有一个如下所示的方法将图像保存为jpeg。 我想保存所有具有相同高度和宽度的图片,而不使其失真 我该怎么做? 请帮忙 public void SaveFileOnDisk(MemoryStream ms, string FileName) { try { string appPath = HttpContext.Current.Request.ApplicationPath; string physicalPath = HttpContext.C

我有一个如下所示的方法将图像保存为jpeg。 我想保存所有具有相同高度和宽度的图片,而不使其失真

我该怎么做? 请帮忙

public void SaveFileOnDisk(MemoryStream ms, string FileName)
{
    try
    {
        string appPath = HttpContext.Current.Request.ApplicationPath;
        string physicalPath = HttpContext.Current.Request.MapPath(appPath);
        string strpath = physicalPath + "\\Images";
        string WorkingDirectory = strpath;


        System.Drawing.Image imgSave = System.Drawing.Image.FromStream(ms);
        Bitmap bmSave = new Bitmap(imgSave);
        Bitmap bmTemp = new Bitmap(bmSave);

        Graphics grSave = Graphics.FromImage(bmTemp);
        grSave.DrawImage(imgSave, 0, 0, imgSave.Width, imgSave.Height);

        bmTemp.Save(WorkingDirectory + "\\" + FileName + ".jpg");


        imgSave.Dispose();
        bmSave.Dispose();
        bmTemp.Dispose();
        grSave.Dispose();
    }
    catch (Exception ex)
    {
        //lblMsg.Text = "Please try again later.";
    }

}

调整图像大小并保存它

Private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
    double srcWidth = img.Source.Width;
    double srcHeight = img.Source.Height;

    double resizeWidth = srcWidth;
    double resizeHeight = srcHeight;

    double aspect = resizeWidth / resizeHeight;

    if (resizeWidth > maxWidth)
    {
        resizeWidth = maxWidth;
        resizeHeight = resizeWidth / aspect;
    }
    if (resizeHeight > maxHeight)
    {
        aspect = resizeWidth / resizeHeight;
        resizeHeight = maxHeight;
        resizeWidth = resizeHeight * aspect;
    }

    img.Width = resizeWidth;
    img.Height = resizeHeight;
}

在保存图像之前,您可以使用此代码将图像大小调整为所需的尺寸,如其他人所述,如果您希望所有图像大小相同而不失真,则需要在保持纵横比的同时调整图像大小。请参见下面的此功能:

public Image ResizeWithSameRatio(Image image, float width, float height)
{
    // the colour for letter boxing, can be a parameter
    var brush = new SolidBrush(Color.Black);

    // target scaling factor
    float scale = Math.Min(width / image.Width, height / image.Height);

    // target image
    var bmp = new Bitmap((int)width, (int)height);
    var graph = Graphics.FromImage(bmp);

    var scaleWidth = (int)(image.Width * scale);
    var scaleHeight = (int)(image.Height * scale);

    // fill the background and then draw the image in the 'centre'
    graph.FillRectangle(brush, new RectangleF(0, 0, width, height));
    graph.DrawImage(image, new Rectangle(((int)width - scaleWidth)/2, ((int)height - scaleHeight)/2, scaleWidth, scaleHeight));

    return bmp;
}
现在,您的使用功能可以大大简化(此处假设为1024x768个目标图像):


注意在变量数量方面增加了简化,并通过使用
use
而不是
Dispose()

进行处理,您能解释一下“相同高度和宽度而不变形”的含义吗?如果原始照片的纵横比(即宽高比)与输出尺寸不同,则会产生失真。避免失真的唯一选择是(1)在一侧或另一侧带有边框的字母装箱,或(2)将目标尺寸设置为一个“固定”尺寸和一个根据原始照片的纵横比变化的尺寸。这现在真的不可能了,是吗?如果你得到一个400 x 300的图像和另一个400 x 100的图像,那么你不能用同样的大小保存它们而不失真。你可以用Graphics.DrawImage()将不合适的字母框括起来。@acadia为什么不接受答案
public void SaveFileOnDisk(MemoryStream ms, string FileName)
{
    try
    {
        string appPath = HttpContext.Current.Request.ApplicationPath;
        string physicalPath = HttpContext.Current.Request.MapPath(appPath);
        string strpath = physicalPath + "\\Images";
        string WorkingDirectory = strpath;

        using (var original = Image.FromStream(ms))
        using (var resized = ResizeWithSameRatio(original, 1024, 768))
        {
            resized.Save(WorkingDirectory + "\\" + FileName + ".jpg");
        }
    }
    catch (Exception ex)
    {
        //lblMsg.Text = "Please try again later.";
    }
}