C#影像處理(3):在映像上加條碼

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   檔案   for   

引入Aspose.BarCode用來產生條碼,方法如下:

 1         /// <summary> 2         /// 產生條碼 3         /// </summary> 4         /// <param name="CodeString">產生條碼的字串</param> 5         /// <param name="Path">條碼儲存的路徑</param> 6         /// <returns>條碼儲存的路徑</returns> 7         public bool CreateBarCode(string CodeString, string Path) 8         { 9             try10             {11                 Aspose.BarCode.BarCodeBuilder builder = new Aspose.BarCode.BarCodeBuilder(CodeString, Symbology.GS1Code128);12                 //string filenameurl = Application.StartupPath + @"\xxx.gif";13                 builder.BorderVisible = false;14                 builder.BarHeight = 10f;15                 builder.BorderWidth = 30f;16                 builder.BorderDashStyle = Aspose.BarCode.BorderDashStyle.Solid;17                 builder.CodeLocation = CodeLocation.Below;18                 MarginsF Margin = new MarginsF(1, 1, 0, 0);19                 builder.Margins = Margin;20                 if (!System.IO.File.Exists(Path))21                 {22                     builder.Save(Path);23                 }24                 else25                 {26                     System.IO.File.Delete(Path);27                     builder.Save(Path);28                 }29                 builder.Dispose();30             }31             catch (Exception ex)32             {33                 return false;34             }35             return true; ;36         }

 

將條碼加入到圖片的指定位置:

 1         /// <summary> 2         /// 圖片上方加條碼,條碼將會被180度反轉 3         /// </summary> 4         /// <param name="Img">待處理圖片</param> 5         /// <param name="ImgBarCode">寫入的條碼</param> 6         /// <param name="UpMargin">180度反轉後條碼頂部距離上邊緣距離</param> 7         /// <param name="RightMargin">條碼最左邊距離右邊緣距離</param> 8         /// <returns></returns> 9         public Bitmap BarCodeUp(Image Img, Image ImgBarCode, int UpMargin, int RightMargin)10         {11             //擷取圖片寬高12             int Width = Img.Width;13             int Height = Img.Height;14             //擷取圖片水平和垂直的解析度15             float dpiX = Img.HorizontalResolution;16             float dpiY = Img.VerticalResolution;17             //建立一個位元影像檔案18             Bitmap BitmapResult = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);19             //設定位元影像檔案的水平和垂直解析度  與Img一致20             BitmapResult.SetResolution(dpiX, dpiY);21             //在位元影像檔案上填充一個矩形框22             Graphics Grp = Graphics.FromImage(BitmapResult);23             System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Width, Height);24             //向矩形框內填充Img25             Grp.DrawImage(Img, 0, 0, Rec, GraphicsUnit.Pixel);26 27 28             //平移Graphics對象29             Grp.TranslateTransform(Width - RightMargin, UpMargin);30             //設定Graphics對象的輸出角度31             Grp.RotateTransform(180);32             //設定條碼填充顏色33             //Brush brush = Brushes.Black;34             //旋轉顯示條碼35             //Grp.DrawString(WriteString, new Font(FontType, FontSize), brush, 0, 0);36             Grp.DrawImage(ImgBarCode, 0, 0);37             //恢複全域變換矩陣38             Grp.ResetTransform();39             Grp.Dispose();40             GC.Collect();41             return BitmapResult;42         }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.