/// <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中的更多相关文章

  1. flex 实现图片播放 方案二 把临时3张图片预加载放入内存

    该方案,是预加载:前一张,当前,下一张图片,一共3张图片放入内存中.这样对内存的消耗可以非常小,加载之后的图片就释放内存. 下面示例一个是类ImagePlayers,一个是index.mxml pac ...

  2. PHP PC端微信扫码支付【模式二】详细教程-附带源码(转)

    博主写这破玩意儿的时候花了大概快两天时间才整体的弄懂逻辑,考虑了一下~还是把所有代码都放出来给大家~抱着开源大无私的精神!谁叫我擅长拍黄片呢?同时也感谢我刚入行时候那些无私帮过我的程序员们! 首先还是 ...

  3. Spring源码系列(二)--bean组件的源码分析

    简介 spring-bean 组件是 Spring IoC 的核心,我们可以使用它的 beanFactory 来获取所需的对象,对象的实例化.属性装配和初始化等都可以交给 spring 来管理. 本文 ...

  4. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  5. 5.2 spring5源码--spring AOP源码分析二--切面的配置方式

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  6. ConcurrentHashMap源码解读二

    接下来就讲解put里面的三个方法,分别是 1.数组初始化方法initTable() 2.线程协助扩容方法helpTransfer() 3.计数方法addCount() 首先是数组初始化,再将源码之前, ...

  7. 十、Spring之BeanFactory源码分析(二)

    Spring之BeanFactory源码分析(二) 前言 在前面我们简单的分析了BeanFactory的结构,ListableBeanFactory,HierarchicalBeanFactory,A ...

  8. 教你阅读 Cpython 的源码(二)

    第二部分:Python解释器进程 在上节教你阅读 Cpython 的源码(一)中,我们从编写Python到执行代码的过程中看到Python语法和其内存管理机制. 在本节,我们将从代码层面去讨论 ,Py ...

  9. 手把手带你阅读Mybatis源码(二)执行篇

    前言 上一篇文章提到了MyBatis是如何构建配置类的,也说了MyBatis在运行过程中主要分为两个阶段,第一是构建,第二就是执行,所以这篇文章会带大家来了解一下MyBatis是如何从构建完毕,到执行 ...

随机推荐

  1. Flask项目之手机端租房网站的实战开发(四)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  2. .net core 修改网站启动端口

    原文:.net core 修改网站启动端口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yenange/article/details/81675 ...

  3. 【博客之星】CSDN2013博客之星--分析和预测

    本文纯属个人见解,多有得罪啊! 具体结果,还是看最后CSDN给的结果吧! 昵称 名字 题材 质量 数量 知名度 预测 阳光岛主 杨刚 Python,Clojure,SAE 很高 346+ 很大 一定( ...

  4. Java Web学习总结(12)——使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  5. CMake - SWIG - 移植动态库

    CMake - SWIG 最后更新日期:2014-04-25 bykagula 阅读前提:<CMake入门(二)>.<同Java的混合编程-SWIG>.Linux的基本操作.j ...

  6. Valgrind的用法

    Valgrind是执行在Linux上一套基于仿真技术的程序调试和分析工具,它包括一个内核──一个软件合成的CPU,和一系列的小工具,每一个工具都能够完毕一项任务──调试.分析,或測试等. Valgri ...

  7. js进阶 14-1 jquery的ajax系列中的load方法的作用是什么

    js进阶 14-1 jquery的ajax系列中的load方法的作用是什么 一.总结 一句话总结:jQuery load()方法作用是从服务器加载数据,是一个简单但强大的AJAX方法. 1.load函 ...

  8. oracle 多行转多列查询

     oracle 多行转多列查询  ---create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);inse ...

  9. <meta name="viewport" content="width=device-width,initial-scale=1.0">

    meta name="viewport" content="width=device-width,initial-scale=1.0" 解释  <meta ...

  10. 洛谷——P3128 [USACO15DEC]最大流Max Flow

    https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to ...