Saving as jpeg from memorystream in c#

前端 未结 2 1948
难免孤独
难免孤独 2021-01-14 06:59

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.

How can I do that

相关标签:
2条回答
  • 2021-01-14 07:02

    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

    0 讨论(0)
  • 2021-01-14 07:20

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

    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 using instead of Dispose().

    0 讨论(0)
提交回复
热议问题