標籤: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 }