簡體   English   中英

在c#中從memorystream保存為jpeg

[英]Saving as jpeg from memorystream in 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目標圖像):

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

請注意,在變量數量方面增加了簡化,並使用using而不是Dispose()進行Dispose()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM