开发者

How to reduce size of a jpeg file before storing it into sql server database?

开发者 https://www.devze.com 2023-03-23 10:49 出处:网络
How can I reduce size of a jpeg file before storing it into sql server database? public byte[] ResizeImageFile(byte[] imageFile, int targetSize) // Set targetSize to 1024

How can I reduce size of a jpeg file before storing it into sql server database?

public byte[] ResizeImageFile(byte[] imageFile, int targetSize) // Set targetSize to 1024
        {
            using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
            {
                Size newSize = new Size(50, 100);
                using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                {
                    using (Graphics canvas = Graphics.FromImage(newImage))
                    {
                        canvas.SmoothingMode = SmoothingMode.AntiAlias;
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                        MemoryStream m = new MemoryStream();
                        newImag开发者_如何学运维e.Save(m, ImageFormat.Jpeg);
                        return m.GetBuffer();
                    }
                }
            }
        }

I get an error at memorystream constructor saying invalid parameters.


Resize it with the utilities in the System.Drawing namespace.
There's a few options and a quick Google gives me these three:

http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx

http://weblogs.asp.net/bleroy/archive/2010/05/03/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish.aspx

https://web.archive.org/web/20211020111640/https://www.4guysfromrolla.com/articles/012203-1.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号