在c#中从内存流保存为jpeg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10661967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:43:34  来源:igfitidea点击:

Saving as jpeg from memorystream in c#

c#imagejpegmemorystream

提问by acadia

I have a method as shown below to save image as jpeg. I want to save all the pictures with the same height and width without it getting distorted.

我有一个如下所示的方法将图像保存为 jpeg。我想保存所有具有相同高度和宽度的图片而不会变形。

How can I do that? Please help

我怎样才能做到这一点?请帮忙

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

}

采纳答案by techno

Resize the Image and Save it

调整图像大小并保存

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

You could use this code to Resize the image to the required Dimention Before saving it

在保存之前,您可以使用此代码将图像调整为所需的尺寸

回答by yamen

As others have mentioned, if you want all images to be the same size without distortion, you're going to need to resize while maintaining the aspect ratio. See this function below:

正如其他人所提到的,如果您希望所有图像的大小相同而不会失真,则需要在保持纵横比的同时调整大小。请参阅下面的此功能:

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

Now your usage function can be significantly simplified (assuming 1024x768 target images here):

现在您的使用函数可以显着简化(这里假设目标图像为 1024x768):

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

Note the added simplification in terms of number of variables, and disposing by using usinginstead of Dispose().

请注意在变量数量方面增加的简化,并使用using代替 进行处理Dispose()