zoukankan      html  css  js  c++  java
  • 图片类处理

    1、读取图片两种方式

    System.Drawing.Bitmap imgFrom = new System.Drawing.Bitmap(fromImg)

    System.Drawing.Image gif = System.Drawing.Image.FromFile(fromImg);

    2、把图片从一个绘制到另一个,首先要先实例化一个Bitmap(背景默认黑色)

     System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(Width, Height);

    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut))
    {
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bmpOut.Width, bmpOut.Height); //填充背景颜色
    g.DrawImage(imgIn, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.GraphicsUnit.Pixel);
    }

    3、如果需要把特定颜色去掉

    bmpOut .MakeTransparent(System.Drawing.Color.FromArgb(252, 252, 252)); //把该颜色设置为透明

    4、图片保存后,一般会比较大,可以设置压缩

    System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
    long[] quality = new long[1] { 100 };
    encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

    bmpOut.Save(fileSaveUrl, GetCodecInfo("image/gif"), encoderParams);

    5、完整代码

    public void Cut(System.Drawing.Bitmap imgIn, int Width, int Height, string fileSaveUrl)
    {
    System.Drawing.Imaging.FrameDimension fd = new System.Drawing.Imaging.FrameDimension(imgIn.FrameDimensionsList[0]);
    if (imgIn.GetFrameCount(fd) > 0)
    {
    imgIn.SelectActiveFrame(fd, 0); //取第一帧图片
    imgIn.MakeTransparent(System.Drawing.Color.FromArgb(252, 252, 252)); //把该颜色设置为透明
    System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(Width, Height);
    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut))
    {
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bmpOut.Width, bmpOut.Height);
    g.DrawImage(imgIn, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.GraphicsUnit.Pixel);
    }

    //以下代码为保存图片时,设置压缩质量
    System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
    long[] quality = new long[1] { 100 };
    encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

    bmpOut.Save(fileSaveUrl, GetCodecInfo("image/gif"), encoderParams);
    }
    }

    6、获得ImageCodecInfo的两种方法

    private System.Drawing.Imaging.ImageCodecInfo GetCodecInfo(string mimeType)
    {
    System.Drawing.Imaging.ImageCodecInfo[] CodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
    foreach (System.Drawing.Imaging.ImageCodecInfo ici in CodecInfo)
    {
    if (ici.MimeType == mimeType)
    return ici;
    }
    return null;
    }

    private ImageCodecInfo GetEncoder(ImageFormat format)
            {
    
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.FormatID == format.Guid)
                    {
                        return codec;
                    }
                }
                return null;
            }
  • 相关阅读:
    [vue][element-ui]mousedown在Dialog上 mouseup在遮罩上时自动关闭弹窗的问题总结
    [ESlint]报错:使用async await时,报(local function)(): Promise<void> Parsing error: Unexpected token function
    [ESlint]报错:Vue eslint Parsing error: Unexpected token
    [CSS]position梳理
    [Node]报错:gyp verb check python checking for Python executable "python2" in the PATH
    [Node]报错:node-sassvendorwin32-x64-79inding.node Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 13.x
    Failed to start OpenLDAP Server Daemon
    与或
    struts2启动报错
    UIButton
  • 原文地址:https://www.cnblogs.com/honzhez/p/6296630.html
Copyright © 2011-2022 走看看