C#生成二维码,把二维码图片放入Excel中
/// <summary>
/// 把图片保存到excel中
/// </summary>
/// <param name="excelFilePath">目标Excel</param>
/// <param name="imageFilePath">保存的图片</param>
/// <param name="width">保存时图片宽度</param>
/// <param name="height">保存时图片高度</param>
/// <param name="col">Excel第几列开始放</param>
/// <param name="row">Excel第几行开始放</param>
public static void InsertImgToExcel(string excelFilePath, string imageFilePath,int width,int height,int col,int row)
{
try
{
FileStream fs = new FileStream(excelFilePath, FileMode.Open, FileAccess.ReadWrite);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
ISheet sheet1 = hssfworkbook.GetSheetAt(0); //map the path to the img folder
string imagesPath = imageFilePath;
//create an image from the path
System.Drawing.Image image = System.Drawing.Image.FromFile(imagesPath);
MemoryStream ms = new MemoryStream();
//pull the memory stream from the image (I need this for the byte array later)
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//the drawing patriarch will hold the anchor and the master information
IDrawing patriarch = sheet1.CreateDrawingPatriarch();
//store the coordinates of which cell and where in the cell the image goes
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 100, col, row, col+3, row+3);
//types are 0, 2, and 3. 0 resizes within the cell, 2 doesn't
anchor.AnchorType = 2;
//add the byte array and encode it for the excel file
int index = hssfworkbook.AddPicture(ms.ToArray(), PictureType.JPEG);
IPicture pict = patriarch.CreatePicture(anchor, LoadImage(imagesPath, hssfworkbook));
pict.Resize();//原图大小 FileStream fs3 = new FileStream(excelFilePath, FileMode.OpenOrCreate);
hssfworkbook.Write(fs3);
fs3.Close();
fs.Close();
}
生成二维码
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="codeNumber">要生成二维码的字符串</param>
/// <param name="size">大小尺寸</param>
/// <returns>二维码图片</returns>
public Bitmap Create_ImgCode(string codeNumber, int size)
{
//创建二维码生成类
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置编码模式
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置编码测量度
qrCodeEncoder.QRCodeScale = size;
//设置编码版本
qrCodeEncoder.QRCodeVersion = 0;
//设置编码错误纠正
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//生成二维码图片
System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber);
return image;
}
用上面方法生成二维码有个问题,当数据量特别大的时候就生成不了。后面换了zxing来生成二维码,生成的数据多一点
public static Bitmap Create(string str)
{
EncodingOptions options = null;
BarcodeWriter writer = null; options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Margin = ,
Width = ,
Height =
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
return writer.Write(str);
}
生成出来的二维码有可能周围的空白处有点多,初步测试可能是由于信息量过大,二维码如果按照原始块间距生成的话会导致超过固定的大小,因此自动减小块间距导致生成的没有固定大小大,留有空白
利用仪器扫描一张图片中某个部分含有二维码,如果图片过大,我遇到过3000*2400左右大小的,用二维码解析根本解析不出来,只有按照二维码的位置进行剪切裁剪后再解析二维码,能实现
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap Cut(Bitmap b)
{
if (b == null)
{
return null;
}
int startX = b.Width * / ;
int startY = ;
int width = b.Width / ;
int height = b.Height / ;
try
{
Bitmap bmpOut = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(, , width, height), new Rectangle(startX, startY, width, height), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
} /// <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 Cut(Bitmap 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, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(, , iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
C#生成二维码,把二维码图片放入Excel中的更多相关文章
- flex 实现图片播放 方案二 把临时3张图片预加载放入内存
该方案,是预加载:前一张,当前,下一张图片,一共3张图片放入内存中.这样对内存的消耗可以非常小,加载之后的图片就释放内存. 下面示例一个是类ImagePlayers,一个是index.mxml pac ...
- PHP PC端微信扫码支付【模式二】详细教程-附带源码(转)
博主写这破玩意儿的时候花了大概快两天时间才整体的弄懂逻辑,考虑了一下~还是把所有代码都放出来给大家~抱着开源大无私的精神!谁叫我擅长拍黄片呢?同时也感谢我刚入行时候那些无私帮过我的程序员们! 首先还是 ...
- Spring源码系列(二)--bean组件的源码分析
简介 spring-bean 组件是 Spring IoC 的核心,我们可以使用它的 beanFactory 来获取所需的对象,对象的实例化.属性装配和初始化等都可以交给 spring 来管理. 本文 ...
- 5.2 Spring5源码--Spring AOP源码分析二
目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...
- 5.2 spring5源码--spring AOP源码分析二--切面的配置方式
目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...
- ConcurrentHashMap源码解读二
接下来就讲解put里面的三个方法,分别是 1.数组初始化方法initTable() 2.线程协助扩容方法helpTransfer() 3.计数方法addCount() 首先是数组初始化,再将源码之前, ...
- 十、Spring之BeanFactory源码分析(二)
Spring之BeanFactory源码分析(二) 前言 在前面我们简单的分析了BeanFactory的结构,ListableBeanFactory,HierarchicalBeanFactory,A ...
- 教你阅读 Cpython 的源码(二)
第二部分:Python解释器进程 在上节教你阅读 Cpython 的源码(一)中,我们从编写Python到执行代码的过程中看到Python语法和其内存管理机制. 在本节,我们将从代码层面去讨论 ,Py ...
- 手把手带你阅读Mybatis源码(二)执行篇
前言 上一篇文章提到了MyBatis是如何构建配置类的,也说了MyBatis在运行过程中主要分为两个阶段,第一是构建,第二就是执行,所以这篇文章会带大家来了解一下MyBatis是如何从构建完毕,到执行 ...
随机推荐
- javasciprt cookies 操作
<script type="text/javascript"> function getCookie(c_name){ if (document.cookie.leng ...
- BAT面试题 - 找一个无序实数数组中的最大差值
题目描写叙述: 一个无序的实数数组a[i].要求求里面大小相邻的实数的差的最大值.比方 double a[]={1,5,4,0.2,100} 这个无序的数组,相邻的数的最大差值为100-5=95. 题 ...
- amazeui学习笔记--css(常用组件14)--缩略图Thumbnail
amazeui学习笔记--css(常用组件14)--缩略图Thumbnail 一.总结 1.基本样式:在 <img> 添加 .am-thumbnail 类:也可以在 <img> ...
- [Angular2 Router] Redirects and Path Matching - Avoid Common Routing Pitfall
In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...
- js变量值传到php(先把php解析成数据)
js变量值传到php(先把php解析成数据) 一.总结 一句话总结:传参数去后台,用ajax,或者原生js方式拼接url.明白原理,洞悉系统是先解析php,再执行html代码和js代码. 二.用aja ...
- python的list和数组的区别
list不是数组(额外安装Pynum) 1)可修改,list数据结构内容可以被程序修改 2)可动态增减,长度不固定 3)list里面的数据项可以是不同类型数据,也可以是list 4)两个list可“链 ...
- 1、初识python
1.linux下运行python脚本时,在第一行通过“#!/usr/bin/env python”指定python h.py <=> ./h.py 具有相同的效果 (h.py需要有执行权限 ...
- width:100%和width:inherit
前几天遇到过这么一个问题.我想让子盒子的宽度等于父盒子的宽度.父盒子宽度为一个具体值比如说200px.我将子盒子宽度设为了100%.按道理说应该是可以等于父盒子的宽度的,但结果并没有,而是通栏了.然后 ...
- 5、linux下应用字符串相关调用函数列举说明
1.函数原型int strcmp(const char *s1,const char *s2);设这两个字符串为s1,s2,规则当s1<s2时,返回为负数当s1=s2时,返回值= 0当s1> ...
- 使用C#版本的gdal库打开hdf文件
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近应同事的请求帮忙研究下使用C#版的gdal库读取hdf文件,今天算是有一点成果,特地做一些记录. 首先是编译C#版 ...