• 压缩远程图片并返回


       public ActionResult Pic()
            {
                var stram = new System.Net.WebClient().OpenRead("http://www.mj100.com/img/index/z_ban02.jpg");
    
    
                byte[] bytes=   MakeSmallImg(stram,500,500);
               
    
                return  File(bytes,"image/jpeg","1.jpg");
            }
            /// <summary>
            /// 压缩图片  15-5-4 by倩
            /// </summary>
            /// <param name="fromFileStream"></param>
            /// <param name="templateWidth"></param>
            /// <param name="templateHeight"></param>
            /// <returns></returns>
            public byte[] MakeSmallImg(System.IO.Stream fromFileStream, System.Double templateWidth, System.Double templateHeight)
            {
                //从文件取得图片对象,并使用流中嵌入的颜色管理信息 
                System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
                //缩略图宽、高 
                System.Double newWidth = myImage.Width, newHeight = myImage.Height;
                //宽大于模版的横图 
                if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
                {
                    if (myImage.Width > templateWidth)
                    {
                        //宽按模版,高按比例缩放 
                        newWidth = templateWidth;
                        newHeight = myImage.Height * (newWidth / myImage.Width);
                    }
                }
                //高大于模版的竖图 
                else
                {
                    if (myImage.Height > templateHeight)
                    {
                        //高按模版,宽按比例缩放 
                        newHeight = templateHeight;
                        newWidth = myImage.Width * (newHeight / myImage.Height);
                    }
                }
                //取得图片大小 
                System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
                //新建一个bmp图片 
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
                //新建一个画板 
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                //设置高质量插值法 
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空一下画布 
                g.Clear(Color.White);
                //在指定位置画图 
                g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
                System.Drawing.GraphicsUnit.Pixel);
    
                using (MemoryStream stream = new MemoryStream())
                {
                    bitmap.Save(stream, ImageFormat.Jpeg);
                    g.Dispose();
                    myImage.Dispose();
                    bitmap.Dispose();
    
                    //输出字节流
                    return stream.ToArray();
    
                }
                
            }
  • 相关阅读:
    Django 部署到Nginx
    Django 初识
    openstack操作之二 restful api
    openstack操作之一 命令行
    虚拟机创建流程中neutron代码分析(三)
    虚拟机创建流程中neutron代码分析(二)
    虚拟机创建流程中neutron代码分析(一)
    nova创建虚拟机源码分析系列之八 compute创建虚机
    nova创建虚拟机源码分析系列之七 传入参数转换成内部id
    Mysql之索引(六)
  • 原文地址:https://www.cnblogs.com/fierceeagle/p/4476027.html
Copyright © 2020-2023  润新知