zoukankan      html  css  js  c++  java
  • c#截取图片

    简单的保存数据流

    <input name="upImg" style=" 350px; height: 25px;" size="38" type="file" />
    /// <summary>
            /// 上传
            /// </summary>
            /// <param name="upImg"></param>
            /// <returns></returns>
            public JsonResult Upload(HttpPostedFileBase upImg)
            {
                string fileName = System.IO.Path.GetFileName(upImg.FileName);
                string filePhysicalPath = Server.MapPath("~/Upload/" + fileName);
                string pic = "";
                int width = 0, height = 0;
                int error = 0;
                try
                {
                    upImg.SaveAs(filePhysicalPath);
                    pic = "/Upload/" + fileName;
                    //得到原图的宽高
                    Image image = new Bitmap(filePhysicalPath);
                    width = image.Width;
                    height = image.Height;
                }
                catch (Exception ex)
                {
                    error = 1;
                }
                return Json(new
                {
                    pic = pic,
                    error = error,
                    width = width,
                    height = height
                });
            }
    保存图片

    来自http://www.jb51.net/article/33622.htm

    1.按照百分比截图

    /// <summary>
    /// 按照比例缩小图片
    /// </summary>
    /// <param name="srcImage">要缩小的图片</param>
    /// <param name="percent">缩小比例</param>
    /// <returns>缩小后的结果</returns>
    public static Bitmap PercentImage(Image srcImage, double percent)
    {
    // 缩小后的高度
    int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
    // 缩小后的宽度
    int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
    try
    {
    // 要保存到的图片
    Bitmap b = new Bitmap(newW, newH);
    Graphics g = Graphics.FromImage(b);
    // 插值算法的质量
    g.InterpolationMode = InterpolationMode.Default;
    g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
    g.Dispose();
    return b;
    }
    catch (Exception)
    {
    return null;
    }
    } 
    按照百分比截图


    2.按照指定像素大小截图

    View Code
    /// <summary>
    /// 按照指定大小缩放图片
    /// </summary>
    /// <param name="srcImage"></param>
    /// <param name="iWidth"></param>
    /// <param name="iHeight"></param>
    /// <returns></returns>
    public static Bitmap SizeImage(Image srcImage, int iWidth, int iHeight)
    {
    try
    {
    // 要保存到的图片
    Bitmap b = new Bitmap(iWidth, iHeight);
    Graphics g = Graphics.FromImage(b);
    // 插值算法的质量
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
    g.Dispose();
    return b;
    }
    catch (Exception)
    {
    return null;
    }
    } 
    按照指定像素大小截图
    3.按照指定像素大小截图(但为了保证图片的原始比例,将对图片从中心进行截取,达到图片不被拉伸的效果
    /// <summary>
    /// 按照指定大小缩放图片,但是为了保证图片宽高比自动截取
    /// </summary>
    /// <param name="srcImage"></param>
    /// <param name="iWidth"></param>
    /// <param name="iHeight"></param>
    /// <returns></returns>
    public static Bitmap SizeImageWithOldPercent(Image srcImage, int iWidth, int iHeight)
    {
    try
    {
    // 要截取图片的宽度(临时图片)
    int newW = srcImage.Width;
    // 要截取图片的高度(临时图片)
    int newH = srcImage.Height;
    // 截取开始横坐标(临时图片)
    int newX = 0;
    // 截取开始纵坐标(临时图片)
    int newY = 0;
    // 截取比例(临时图片)
    double whPercent = 1;
    whPercent = ((double)iWidth / (double)iHeight) * ((double)srcImage.Height / (double)srcImage.Width);
    if (whPercent > 1)
    {
    // 当前图片宽度对于要截取比例过大时
    newW = int.Parse(Math.Round(srcImage.Width / whPercent).ToString());
    }
    else if (whPercent < 1)
    {
    // 当前图片高度对于要截取比例过大时
    newH = int.Parse(Math.Round(srcImage.Height * whPercent).ToString());
    }
    if (newW != srcImage.Width)
    {
    // 宽度有变化时,调整开始截取的横坐标
    newX = Math.Abs(int.Parse(Math.Round(((double)srcImage.Width - newW) / 2).ToString()));
    }
    else if (newH == srcImage.Height)
    {
    // 高度有变化时,调整开始截取的纵坐标
    newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)newH) / 2).ToString()));
    }
    // 取得符合比例的临时文件
    Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH);
    // 保存到的文件
    Bitmap b = new Bitmap(iWidth, iHeight);
    Graphics g = Graphics.FromImage(b);
    // 插值算法的质量
    g.InterpolationMode = InterpolationMode.Default;
    g.DrawImage(cutedImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel);
    g.Dispose();
    return b;
    }
    catch (Exception)
    {
    return null;
    }
    } 
    按照指定像素大小截图
    4.jpeg图片质量压缩,压缩的比例参数在1-100之间。(适量的压缩对于肉眼来说没有什么明显的区别,但是能够大大的减小图片的占用大小
    /// <summary>
    /// jpeg图片压缩
    /// </summary>
    /// <param name="sFile"></param>
    /// <param name="outPath"></param>
    /// <param name="flag"></param>
    /// <returns></returns>
    public static bool GetPicThumbnail(string sFile, string outPath, int flag)
    {
    System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
    ImageFormat tFormat = iSource.RawFormat;
    //以下代码为保存图片时,设置压缩质量
    EncoderParameters ep = new EncoderParameters();
    long[] qy = new long[1];
    qy[0] = flag;//设置压缩的比例1-100
    EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
    ep.Param[0] = eParam;
    try
    {
    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
    ImageCodecInfo jpegICIinfo = null;
    for (int x = 0; x < arrayICI.Length; x++)
    {
    if (arrayICI[x].FormatDescription.Equals("JPEG"))
    {
    jpegICIinfo = arrayICI[x];
    break;
    }
    }
    if (jpegICIinfo != null)
    {
    iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径
    }
    else
    {
    iSource.Save(outPath, tFormat);
    }
    return true;
    }
    catch
    {
    return false;
    }
    finally
    {
    iSource.Dispose();
    iSource.Dispose();
    }
    } 
    jpeg图片质量压缩


    PS:之上用的CutImage方法的补充

    View Code
    /// <summary>
    /// 剪裁 -- 用GDI+
    /// </summary>
    /// <param name="b">原始Bitmap</param>
    /// <param name="StartX">开始坐标X</param>
    /// <param name="StartY">开始坐标Y</param>
    /// <param name="iWidth">宽度</param>
    /// <param name="iHeight">高度</param>
    /// <returns>剪裁后的Bitmap</returns>
    public static Bitmap CutImage(Image b, int StartX, int StartY, int iWidth, int iHeight)
    {
    if (b == null)
    {
    return null;
    }
    int w = b.Width;
    int h = b.Height;
    if (StartX >= w || StartY >= h)
    {
    // 开始截取坐标过大时,结束处理
    return null;
    }
    if (StartX + iWidth > w)
    {
    // 宽度过大时只截取到最大大小
    iWidth = w - StartX;
    }
    if (StartY + iHeight > h)
    {
    // 高度过大时只截取到最大大小
    iHeight = h - StartY;
    }
    try
    {
    Bitmap bmpOut = new Bitmap(iWidth, iHeight);
    Graphics g = Graphics.FromImage(bmpOut);
    g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
    g.Dispose();
    return bmpOut;
    }
    catch
    {
    return null;
    }
    }
    CutImage方法的补充
  • 相关阅读:
    pandas模块
    27.mysql数据库之约束
    nump模块
    26.mysql数据库基础
    24.IO模型
    23.并发编程之协程
    第五十三篇 并发编程之多进程续
    第五十二篇 操作系统简史——多道技术
    第五十一篇 并发编程——多进程
    第四十九篇 socket套接字编程
  • 原文地址:https://www.cnblogs.com/danlis/p/5504238.html
Copyright © 2011-2022 走看看