欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 10:06:04
复制代码 代码如下:/// /// 生成二维码/// ///

复制代码 代码如下:

/// <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("//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."));