C#代码修改图片分辨率大小

本人在此之前,曾看过一些博客或者其他网上的文章,同时我也参考和借鉴、以及使用某个博主的代码进行修改过,在此忘了那个博主的博客地址了,有什么不合理之处请见谅!

本方法是传入一个图片的图片流(本人使用的是MemoryStream ),最后返回的也是MemoryStream 对象,代码如下:

        public static MemoryStream AdjustPhoto(int toWidth, int toHeight, MemoryStream stream, int maxWidth, int maxHeight)
        {
            Image originalImage = Image.FromStream(stream);

            //根据图片大小获取新图片从原图片截取的区域
            int x, y, w, h;
            if (toHeight > 0)
            {
                if (toWidth > 0)
                {
                    if (originalImage.Width > toWidth && originalImage.Height > toHeight)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;

                        if (h > toHeight)
                        {
                            h = toHeight;
                            w = toHeight * originalImage.Width / originalImage.Height;
                            x = (toWidth - w) / 2;
                            y = 0;
                        }
                        else
                        {
                            x = 0;
                            y = (toHeight - h) / 2;
                        }
                    }
                    else if (originalImage.Width > toWidth)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;
                        x = 0;
                        y = (toHeight - h) / 2;
                    }
                    else if (originalImage.Height > toHeight)
                    {
                        h = toHeight;
                        w = toHeight * originalImage.Width / originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = 0;
                    }
                    else
                    {
                        w = originalImage.Width;
                        h = originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = (toHeight - h) / 2;
                    }
                }
                else
                {
                    if (originalImage.Height > maxHeight)
                    {
                        toWidth = toHeight * originalImage.Width / originalImage.Height;
                        x = 0;
                        y = 0;
                        w = toWidth;
                        h = toHeight;

                    }
                    else
                    {
                        x = 0;
                        y = 0;
                        w = originalImage.Width;
                        h = originalImage.Height;
                        toWidth = originalImage.Width;
                        toHeight = originalImage.Height;
                    }
                }
            }
            else
            {
                if (originalImage.Width > maxWidth)
                {
                    toHeight = toWidth * originalImage.Height / originalImage.Width;
                    x = 0;
                    y = 0;
                    w = toWidth;
                    h = toHeight;

                }
                else
                {
                    x = 0;
                    y = 0;
                    w = originalImage.Width;
                    h = originalImage.Height;
                    toWidth = originalImage.Width;
                    toHeight = originalImage.Height;
                }
            }
            Bitmap bm = new Bitmap(toWidth, toHeight);
            Graphics g = Graphics.FromImage(bm);

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.Clear(Color.White);
            g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);

            long[] quality = new long[1];
            quality[0] = 80;

            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
            ImageCodecInfo jpegICI = null;
            for (int i = 0; i < arrayICI.Length; i++)
            {
                if (arrayICI[i].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[i];//设置JPEG编码
                    break;
                }
            }
            MemoryStream ms = new MemoryStream();
            if (jpegICI != null)
            {
                bm.Save(ms, jpegICI, encoderParams);
            }

            bm.Dispose();
            originalImage.Dispose();
            g.Dispose();
            return ms;
        }

在这代码中,其中需要创建一个画板,并设置相关参数:

            Bitmap bm = new Bitmap(toWidth, toHeight);
            Graphics g = Graphics.FromImage(bm);

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

接着,就将原始图片画进画板中:

            g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);

最后,   获取图形编码器的信息,将图片保存为jpg格式的内存流(MemoryStream):

            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
            ImageCodecInfo jpegICI = null;
            for (int i = 0; i < arrayICI.Length; i++)
            {
                if (arrayICI[i].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[i];//设置JPEG编码
                    break;
                }
            }
            MemoryStream ms = new MemoryStream();
            if (jpegICI != null)
            {
                bm.Save(ms, jpegICI, encoderParams);
            }



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wingahi/article/details/38360141
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭