国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#生成二維碼,把二維碼圖片放入Excel中

2019-11-17 02:31:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

C#生成二維碼,把二維碼圖片放入Excel

  /// <summary>        /// 把圖片保存到excel中        /// </summary>        /// <param name="excelFilePath">目標(biāo)Excel</param>        /// <param name="imageFilePath">保存的圖片</param>        /// <param name="width">保存時(shí)圖片寬度</param>        /// <param name="height">保存時(shí)圖片高度</param>        /// <param name="col">Excel第幾列開(kāi)始放</param>        /// <param name="row">Excel第幾行開(kāi)始放</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)        {            //創(chuàng)建二維碼生成類            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();            //設(shè)置編碼模式            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;            //設(shè)置編碼測(cè)量度            qrCodeEncoder.QRCodeScale = size;            //設(shè)置編碼版本            qrCodeEncoder.QRCodeVersion = 0;            //設(shè)置編碼錯(cuò)誤糾正            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;            //生成二維碼圖片            System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber);            return image;        }

  用上面方法生成二維碼有個(gè)問(wèn)題,當(dāng)數(shù)據(jù)量特別大的時(shí)候就生成不了。后面換了zxing來(lái)生成二維碼,生成的數(shù)據(jù)多一點(diǎn)

 1 public static Bitmap Create(string str) 2         { 3             EncodingOptions options = null; 4             BarcodeWriter writer = null; 5  6             options = new QrCodeEncodingOptions 7             { 8                 DisableECI = true, 9                 CharacterSet = "UTF-8",10                 Margin = 0,11                 Width = 125,12                 Height = 12513             };14             writer = new BarcodeWriter();15             writer.Format = BarcodeFormat.QR_CODE;16             writer.Options = options;17             return writer.Write(str);18         }

生成出來(lái)的二維碼有可能周圍的空白處有點(diǎn)多,初步測(cè)試可能是由于信息量過(guò)大,二維碼如果按照原始?jí)K間距生成的話會(huì)導(dǎo)致超過(guò)固定的大小,因此自動(dòng)減小塊間距導(dǎo)致生成的沒(méi)有固定大小大,留有空白

利用儀器掃描一張圖片中某個(gè)部分含有二維碼,如果圖片過(guò)大,我遇到過(guò)3000*2400左右大小的,用二維碼解析根本解析不出來(lái),只有按照二維碼的位置進(jìn)行剪切裁剪后再解析二維碼,能實(shí)現(xiàn)

  /// <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 * 3 / 4;            int startY = 0;            int width = b.Width / 4;            int height = b.Height / 4;            try            {                Bitmap bmpOut = new Bitmap(width, height, PixelFormat.Format24bpPRgb);                Graphics g = Graphics.FromImage(bmpOut);                g.DrawImage(b, new Rectangle(0, 0, 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">開(kāi)始坐標(biāo)X</param>        /// <param name="StartY">開(kāi)始坐標(biāo)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(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);                g.Dispose();                return bmpOut;            }            catch            {                return null;            }        }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盘锦市| 含山县| 西青区| 南康市| 叶城县| 商南县| 昆明市| 象山县| 新宁县| 乃东县| 黎平县| 文登市| 南康市| 浮山县| 探索| 洛隆县| 昂仁县| 资源县| 清水县| 常宁市| 望都县| 西乡县| 隆德县| 平舆县| 江达县| 镇原县| 陆丰市| 绥阳县| 伊吾县| 勐海县| 班玛县| 茂名市| 夏津县| 信宜市| 隆子县| 三都| 牙克石市| 周至县| 长宁县| 时尚| 本溪|