zoukankan      html  css  js  c++  java
  • 上传图片并显示缩略图的最简单方法(c#)

    private void fileUpload_Click(object sender, System.EventArgs e)
            
    {
                
    // 模拟数据库里取出byte[]再显示缩略,
                
    // 模拟方法:先上传,把stream转成byte[],再把byte[]放在stream里,再输出

                
    // 上传
                System.IO.Stream fs = jpgUpload.PostedFile.InputStream;
                
    int nBytes          = jpgUpload.PostedFile.ContentLength;
                
    byte[] ByteArray    = new byte[nBytes];
                
    int nBytesRead      = fs.Read(ByteArray, 0, nBytes);
                MemoryStream mBytes  
    = new MemoryStream(ByteArray,0,nBytes);
                
                
    // 转为stream,处理缩略
                System.Drawing.Image _img;
                _img 
    = System.Drawing.Image.FromStream(mBytes);
                System.Drawing.Image _thumbImg 
    = _img.GetThumbnailImage(Convert.ToInt32(_img.Width* 0.3),Convert.ToInt32( _img.Height * 0.3),null, IntPtr.Zero);

                
    // 显示到客户端
                Response.ContentType    = this.jpgUpload.PostedFile.ContentType;
                MemoryStream MemStream  
    = new MemoryStream(); 
                _thumbImg.Save(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
                MemStream.WriteTo(Response.OutputStream); 
                Response.Flush();
            }

    注释都在里面,不用说明了吧,重要的是GetThumbnailImage这个方法.
  • 相关阅读:
    安卓手机!用swiper做轮播效果,两张图片之间会有一个像素的空白
    手机端swiper快速滑动会有白屏
    axios请求下载excel文件
    人人都能学会的webpack5搭建vue3项目(二)配置Vue
    人人都能学会的webpack5搭建vue3.0项目,可应用于生产环境 (一)
    mybatis 生成 mapper文件
    超强工具类系列
    mybatis 自动生成mapper文件
    面试
    linux安装mysql8.0.25
  • 原文地址:https://www.cnblogs.com/syveen/p/267270.html
Copyright © 2011-2022 走看看