c#裁剪图片后使用zxing生成二维码示例分享

 更新时间:2014年01月17日 11:19:08   作者:  
这篇文章主要介绍了c#裁剪图片后使用zxing生成二维码的示例,大家参考使用吧

复制代码 代码如下:

/// <summary>
/// 生成二维码
/// </summary>
/// <param name="fileName">生成二维码路径</param>
/// <param name="url">生成的内容</param>
/// <param name="width">二维码宽</param>
/// <param name="height">二维码高</param>
/// <param name="userFace">需生成的Logo图片</param>
/// <returns></returns>
private Bitmap GetCodeImgUrl(string fileName, string url, int width, int height, string userFace)
{

    BarcodeWriter writer = new BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Renderer = new BitmapRenderer
        {
            Foreground = Color.Black
        },
        Options = new ZXing.QrCode.QrCodeEncodingOptions
        {
            DisableECI = true,
            Height = height,
            Width = width,
            Margin = 0,
            CharacterSet = "UTF-8",
            ErrorCorrection = ErrorCorrectionLevel.M
        }
    };

    Bitmap bitmap = writer.Write(url);
    if (!string.IsNullOrEmpty(userFace))
    {
        Bitmap bits = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(userFace);
        if (bits != null)
        {                   
            //剪裁一个80*80的Logo图片
            ImageCut img = new ImageCut(0, 0, 80, 80);
            System.Drawing.Bitmap icon = img.KiCut(bits);
            //userFace_b.jpg是一个边框的图片
            Bitmap bits2 = new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromFile(Application.StartupPath + "/user/userFace_b.jpg"), 84, 84);
            if (icon != null)
            {
                try
                { 
                    //画了2个边框,一个是logo,一个在logo周围加了一个边框
                    using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
                    {
                        graphics.DrawImage(bits2, (bitmap.Width - bits2.Width) / 2, (bitmap.Height - bits2.Height) / 2);
                        graphics.DrawImage(icon, (bitmap.Width - icon.Width) / 2, (bitmap.Height - icon.Height) / 2);

                    }

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    icon.Dispose();
                    GC.Collect();

                }
            }
            bitmap.Save(fileName, ImageFormat.Jpeg);
        }

    }

    return bitmap;
}

复制代码 代码如下:

public class ImageCut
  {

      /// <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 Bitmap KiCut(Bitmap b)
      {
          if (b == null)
          {
              return null;
          }
          int w = b.Width;
          int h = b.Height;
          int intWidth = 0;
          int intHeight = 0;
          if (h * Width / w > Height)
          {
              intWidth = Width;
              intHeight = h * Width / w;

          }
          else if (h * Width / w < Height)
          {
              intWidth = w * Height / h;
              intHeight = Height;

          }
          else
          {
              intWidth = Width;
              intHeight = Height;
          }

          Bitmap bmpOut_b = new System.Drawing.Bitmap(b, intWidth, intHeight);
          w = bmpOut_b.Width;
          h = bmpOut_b.Height;
        

          if (X >= w || Y >= h)
          {
              return null;
          }

          if (X + Width > w)
          {
              Width = w - X;
          }
          else
          {
              X = (w-Width) / 2;
          }

          if (Y + Height > h)
          {
              Height = h - Y;
          }
        

 

          try
          {
              Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);            
              Graphics g = Graphics.FromImage(bmpOut);
              g.DrawImage(bmpOut_b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
              g.Dispose();

              return bmpOut;
          }
          catch
          {
              return null;
          }
      }

      public int X = 0;
      public int Y = 0;
      public int Width = 120;
      public int Height = 120;
      public ImageCut(int x, int y, int width, int heigth)
      {
          X = x;
          Y = y;
          Width = width;
          Height = heigth;
      }
  }

复制代码 代码如下:

  private void btnSubmit_Click(object sender, EventArgs e)
        {
            string UserId = "1245460396";   

            string curFilePath = "/user/";

            string curFileName_b = "DimensionalPig_" + UserId + "_b";
            string path = Application.StartupPath + curFilePath;
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }
            string fileName_b = Application.StartupPath + curFilePath + "/" + curFileName_b + ".jpg";//获得上传文件名

            string UserUrl = string.Format("https://www.jb51.net/u{0}", UserId.Trim());
            string userFace_b = Application.StartupPath + "/user/" + UserId + "_b.jpg";

            Bitmap bitmap_b = GetCodeImgUrl(fileName_b.Replace("_b.", "_b_ewm."), UserUrl, 400, 400, userFace_b);
            this.p.Image =(System.Drawing.Image)bitmap_b;
this.p.Image.Save(fileName_b.Replace("_b.", "_b_ewm."));

相关文章

  • c# 适配器模式

    c# 适配器模式

    适配器模式:将一个类的接口转换成客户希望的另一个接口,Adapter使原本由于接口不兼容而不能一起工作的那些类可以一起工作
    2012-10-10
  • C#操作windows系统进程的方法

    C#操作windows系统进程的方法

    这篇文章主要介绍了C#操作windows系统进程的方法,涉及C#针对windows操作系统进程的创建与关闭的技巧,需要的朋友可以参考下
    2015-04-04
  • C#实现强制关闭当前程序进程

    C#实现强制关闭当前程序进程

    这篇文章主要介绍了C#实现强制关闭当前程序进程,本文直接给出实现代码,可以实现完全Kill掉不留痕迹,需要的朋友可以参考下
    2015-06-06
  • C#判断ip地址是否可以ping的通

    C#判断ip地址是否可以ping的通

    这篇文章主要介绍了ip地址是否可以ping的通的方法,使用到了C#,需要的朋友可以参考下
    2014-06-06
  • c#操作xml帮助类分享(xml增删改查)

    c#操作xml帮助类分享(xml增删改查)

    c#操作xml帮助类XMLHelper源码分享,实现对XML文档的创建,及节点和属性的增、删、改、查
    2014-01-01
  • C#实现鼠标移动到曲线图上显示值的方法

    C#实现鼠标移动到曲线图上显示值的方法

    这篇文章主要介绍了C#实现鼠标移动到曲线图上显示值的方法,是C#的WinForm窗体程序设计中非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • C# 删除数组内的某个值、一组值方法详解

    C# 删除数组内的某个值、一组值方法详解

    在本篇文章里小编给大家整理的是关于C# 如何删除数组内的某个值、一组值的相关知识点,需要的朋友们学习下。
    2020-03-03
  • C# RGB图像和灰度图像互转的实现

    C# RGB图像和灰度图像互转的实现

    在我们的图像类型教程中定义了RGB颜色模型和灰度格式,本文主要介绍了C# RGB图像和灰度图像互转的实现,文中通过代码介绍的非常清楚,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • unity实现手游虚拟摇杆

    unity实现手游虚拟摇杆

    这篇文章主要为大家详细介绍了unity实现手游虚拟摇杆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • c# 对CSV文件操作(写入、读取、修改)

    c# 对CSV文件操作(写入、读取、修改)

    这篇文章主要介绍了c# 如何对CSV文件操作,帮助大家更好的理解和学习C#,感兴趣的朋友可以了解下
    2020-08-08

最新评论