ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 278.2k

A generic error occurred in GDI error when save bar code ima

Dec 31 2017 4:38 PM

Problem

when i press button preview to show barcode it give me error as following
 
  1.   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)  
  2.   
  3.    at System.Drawing.Image.Save(String filename, ImageFormat format)  
  4.   
  5.    at System.Drawing.Image.Save(String filename)  
  6.   
  7.    at BarcodesPrinting.BtnPreviwe_Click(Object sender, EventArgs e) in D:\Engaz\TABSolutions\MiniTAB\Trunk\Code\Tab\BarCode\Rpts\BarcodesPrinting.cs:line 707  
  8.   
  9.    at System.Windows.Forms.Control.OnClick(EventArgs e)  
  10.   
  11.    at System.Windows.Forms.Button.OnClick(EventArgs e)  
  12.   
  13.    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)  
  14.   
  15.    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)  
  16.   
  17.    at System.Windows.Forms.Control.WndProc(Message& m)  
  18.   
  19.    at System.Windows.Forms.ButtonBase.WndProc(Message& m)  
  20.   
  21.    at System.Windows.Forms.Button.WndProc(Message& m)  
  22.   
  23.    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)  
  24.   
  25.    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)  
  26.   
  27.    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)  
  28.   
the error display on this line
  1. picItem.Image.Save(BarcodePath);  
my code
 
  1. BarcodePath = Application.StartupPath + @"\images\barcode.png";
  2. private void BtnPreviwe_Click(object sender, EventArgs e)  
  3.         {  
  4.             
  5.             CreateBarcode(txtFromItem.Text.Trim());  
  6.             picItem.Image.Save(BarcodePath);  
  7.         }  
createbarcode function
  1. private void CreateBarcode(string barcodeText = "")  
  2.         {  
  3.   
  4.             string sql = " select * from BarCodeDesign where SizeCode=" + txtBarCodeDesign.Text;  
  5.             DataTable tbRes = DataAccess.ExecuteDataTable(sql);  
  6.             if (tbRes.Rows.Count > 0)  
  7.             {  
  8.                 sizeCode = Convert.ToInt32(txtBarCodeDesign.Text);  
  9.                 int W = Convert.ToInt32(string.IsNullOrEmpty(Convert.ToString(tbRes.Rows[0]["BarcodeWidth"])) ? "100" : Convert.ToString(tbRes.Rows[0]["BarcodeWidth"]));  
  10.                 int H = Convert.ToInt32(string.IsNullOrEmpty(Convert.ToString(tbRes.Rows[0]["BarcodeHeight"])) ? "100" : Convert.ToString(tbRes.Rows[0]["BarcodeHeight"]));  
  11.                 width = W;  
  12.                 height = H;  
  13.   
  14.                 AlignmentPositions Align = AlignmentPositions.CENTER;  
  15.   
  16.                 if (!string.IsNullOrEmpty(Convert.ToString(Convert.ToString(tbRes.Rows[0]["BarcodeAlign"]))))  
  17.                 {  
  18.                     switch (Convert.ToInt32(Convert.ToString(Convert.ToString(tbRes.Rows[0]["BarcodeAlign"]).Trim().ToLower())))  
  19.                     {  
  20.                         case 2: Align = AlignmentPositions.LEFT; break;  
  21.                         case 3: Align = AlignmentPositions.RIGHT; break;  
  22.                         default: Align = AlignmentPositions.CENTER; break;  
  23.                     }  
  24.                     BarcodeAlign = Convert.ToInt32(Convert.ToString(Convert.ToString(tbRes.Rows[0]["BarcodeAlign"]).Trim().ToLower()));  
  25.                 }  
  26.   
  27.                 TYPE type = TYPE.UNSPECIFIED;  
  28.   
  29.                 if (!string.IsNullOrEmpty(Convert.ToString(tbRes.Rows[0]["BarcodeFont"])))  
  30.                 {  
  31.                     switch (Convert.ToInt32(Convert.ToString(tbRes.Rows[0]["BarcodeFont"]).Trim()))  
  32.                     {  
  33.                         case 1: type = TYPE.UPCA; break;  
  34.                         case 2: type = TYPE.UPCE; break;  
  35.                         case 3: type = TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;  
  36.                         case 4: type = TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;  
  37.                         case 5: type = TYPE.EAN13; break;  
  38.                         case 6: type = TYPE.JAN13; break;  
  39.                         case 7: type = TYPE.EAN8; break;  
  40.                         case 8: type = TYPE.ITF14; break;  
  41.                         case 9: type = TYPE.Codabar; break;  
  42.                         case 10: type = TYPE.PostNet; break;  
  43.                         case 11: type = TYPE.BOOKLAND; break;  
  44.                         case 12: type = TYPE.CODE11; break;  
  45.                         case 13: type = TYPE.CODE39; break;  
  46.                         case 14: type = TYPE.CODE39Extended; break;  
  47.                         case 15: type = TYPE.CODE93; break;  
  48.                         case 16: type = TYPE.LOGMARS; break;  
  49.                         case 17: type = TYPE.MSI_Mod10; break;  
  50.                         case 18: type = TYPE.Interleaved2of5; break;  
  51.                         case 19: type = TYPE.Standard2of5; break;  
  52.                         case 20: type = TYPE.CODE128; break;  
  53.                         case 21: type = TYPE.CODE128A; break;  
  54.                         case 22: type = TYPE.CODE128B; break;  
  55.                         case 23: type = TYPE.CODE128C; break;  
  56.                         case 24: type = TYPE.TELEPEN; break;  
  57.                         case 25: type = TYPE.FIM; break;  
  58.                         default: MessageBox.Show("Please specify the encoding type."); break;  
  59.                     }  
  60.   
  61.                     BarcodeFont = Convert.ToInt32(Convert.ToString(tbRes.Rows[0]["BarcodeFont"]).Trim());  
  62.                 }  
  63.                 try  
  64.                 {  
  65.                     if (type != TYPE.UNSPECIFIED)  
  66.                     {  
  67.                         if (string.IsNullOrEmpty(Convert.ToString(tbRes.Rows[0]["ShowCode"])))  
  68.                         {  
  69.                             b.IncludeLabel = false;  
  70.                         }  
  71.                         else  
  72.                         {  
  73.                             b.IncludeLabel = Convert.ToBoolean(tbRes.Rows[0]["ShowCode"]);  
  74.                         }  
  75.   
  76.                         ShowCode = b.IncludeLabel;  
  77.                         b.Alignment = Align;  
  78.                         //b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), this.cmbBarcodeRotate.SelectedValue.ToString(), true);  
  79.                         //label alignment and position  
  80.                         switch (Convert.ToInt32(Convert.ToString(tbRes.Rows[0]["NumbersLocation"]).Trim().ToUpper()))  
  81.                         {  
  82.                             case 2: b.LabelPosition = LabelPositions.BOTTOMLEFT; break;  
  83.                             case 3: b.LabelPosition = LabelPositions.BOTTOMRIGHT; break;  
  84.                             case 4: b.LabelPosition = LabelPositions.TOPCENTER; break;  
  85.                             case 5: b.LabelPosition = LabelPositions.TOPLEFT; break;  
  86.                             case 6: b.LabelPosition = LabelPositions.TOPRIGHT; break;  
  87.                             default: b.LabelPosition = LabelPositions.BOTTOMCENTER; break;  
  88.                         }  
  89.   
  90.                         NumbersLocation = Convert.ToInt32(Convert.ToString(tbRes.Rows[0]["NumbersLocation"]).Trim().ToUpper());  
  91.                         //===== Encoding performed here =====  
  92.                         //barcode.Image = b.Encode(type, this.txtBarcodeTry.Text.Trim(), this.btnForeColor.BackColor, this.btnBackColor.BackColor, W, H);  
  93.   
  94.                         if (string.IsNullOrEmpty(barcodeText))  
  95.                         {  
  96.                             picItem.Image = b.Encode(type, Convert.ToString(tbRes.Rows[0]["BarcodeText"]).Trim(), W, H);  
  97.                         }  
  98.                         else  
  99.                         {  
  100.                             picItem.Image = b.Encode(type, barcodeText, W, H);  
  101.                         }  
  102.   
  103.                         BarcodeText = Convert.ToString(tbRes.Rows[0]["BarcodeText"]).Trim();  
  104.   
  105.                          
  106.   
  107.                 }  
  108.                 catch (Exception ex)  
  109.                 {  
  110.                     MessageBox.Show(ex.Message);  
  111.                 }  
  112.             }  
  113.         }  
 

the bar code library for encode function attached with question

How to solve this error please ?


Attachment: barcode_library.rar

Answers (1)