【问题标题】:Control the image file size after re-size and add watermark调整大小并添加水印后控制图像文件大小
【发布时间】:2012-12-09 05:13:57
【问题描述】:

我正在调整图像大小并向图像添加水印。并返回图像。这是我的代码。

Image resizedImage = ResizeImage(original6, new Size(500, 375));

重新调整大小函数:

public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size)
{
    int newWidth;
    int newHeight;
    if (true)
    {
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;
        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);
    }
    else
    {
        newWidth = size.Width;
        newHeight = size.Height;
    }
    System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics graphicsHandle = Graphics.FromImage(newImage))
    {
        graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
    }


    System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(newImage, size.Width, size.Height);// create bitmap with same size of Actual image
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);

    SolidBrush brush = new SolidBrush(Color.FromArgb(113, 255, 255, 255));
    //Adding watermark text on image
    g.DrawString("My watermark", new Font("Arial", 16, FontStyle.Bold), brush, 5, 100);

    return bitmapimage;
}

我正在检索带有水印的新调整大小的图像并保存为新图像文件。

resized6.Save(Server.MapPath(sSavePath + ownerRef + "Pic6v2" + ".jpg"));

这工作正常。但是我无法控制文件大小。 当我的原始 JPG 只有 45kb 但我重新调整大小的新图像是 500kb 时。如何减小文件大小。? 信息:原始分辨率(400x300 px)和新图像(500x375px)

【问题讨论】:

    标签: c# asp.net image image-resizing


    【解决方案1】:

    我不记得这件事了,但文件大小通常与 JPEG 质量设置有关。您还需要确保将其保存为实际的 jpg,而不是位图,我看不出您正在这样做..

    另请参阅:C# Simple Image Resize : File Size Not Shrinking

    JPEG 质量设置: http://msdn.microsoft.com/en-us/library/bb882583.aspx

    【讨论】:

      【解决方案2】:

      您可以更改 JPEG 的质量以获得更小的文件大小。见

      http://msdn.microsoft.com/en-us/library/bb882583.aspx

      还有

      Quality of a saved JPG in C#

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-27
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多