2010-08-11 51 views
0

存在表面圖像,並且在圖像上爲文本寫入日期184行。 因此,預計會看到184個不同的文本寫入圖像文件用所有相同的背景圖像生成。 (該代碼聲明如下...)清理圖像水印上的表面

問題是,第一個文本是寫入所有184個不同的數據..我想我必須刪除循環中的東西。但那是什麼?

for (int i = 0; i < dt.Rows.Count; i++) { 
      date = Convert.ToDateTime(dt.Rows[i]["PAYMENT_DATE"]); 
      branchCode = Convert.ToInt32(dt.Rows[i]["BRANCH_CODE"]); 
      refNum = Convert.ToInt32(dt.Rows[i]["REF_NUM"]); 
      accountNumber = Convert.ToInt32(dt.Rows[i]["ACCOUNT_NUMBER"]); 
      email = dt.Rows[i]["EMAIL"].ToString(); 
      tableCode = dt.Rows[i]["CUSTOMER_TABLE_CODE"].ToString(); 

      TranLogKey logKey = new TranLogKey(date, branchCode, refNum); 
      TranLogEntry entry = Log.SelectLogEntry(logKey, false); 
      if (Intertech.Core.Framework.Context.CurrentContext.LogEntry == null) 
       Intertech.Core.Framework.Context.CurrentContext.LogEntry = entry; 

      try { 
       receiptText = TransactionManager.GenerateReceipt(true, logKey, null, null, accountNumber, false); 
      } 
      catch (Exception exp) { 
       continue; 
      } 

      if (receiptText != null) { 
       if (receiptText.IndexOf("SURETTİR\r\n") != -1) 
        receiptText = receiptText.Substring(receiptText.IndexOf("SURETTİR\r\n") + 10).Trim(); 

       if (receiptText.IndexOf("İşlemi Yapan") != -1) 
        receiptText = receiptText.Substring(0, receiptText.IndexOf("İşlemi Yapan")); 
       if (receiptText.IndexOf("MÜŞTERİ İMZASI") != -1) 
        receiptText = receiptText.Substring(0, receiptText.IndexOf("MÜŞTERİ İMZASI")); 

       Bitmap bmp = (Bitmap)Bitmap.FromFile(imageDir); 
       Graphics g = Graphics.FromImage(bmp); 
       SizeF size; 
       Font font = new Font("Courier New", 8, FontStyle.Regular); 
       byte ALPHA = 200; 
       size = g.MeasureString(receiptText, font); 
       Bitmap waterbmp = new Bitmap((int)size.Width, (int)size.Height); 
       Graphics waterg = Graphics.FromImage(waterbmp); 
       waterg.DrawString(receiptText, font, new SolidBrush(System.Drawing.Color.Black), 2, 2); 
       DrawWatermark(ref waterbmp, ref bmp, LeftIndex, TopIndex, ALPHA); 
       bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 

       try { 


         GoServices.Core.SendMailOutside("The Portugal Life", "[email protected]", 
            "[email protected]", " e-Wish " + email, "", new string[] { "dekont.jpg" }, new object[] { ms.ToArray() }); 

         LogNotificationState("K", refNum, accountNumber, 2, true, null, tableCode); 

       } 
       catch (Exception ex) { 
        LogNotificationState("K", 0, -1, 2, false, ex, tableCode); 
       } 

      } 



private static void DrawWatermark(ref Bitmap watermark_bm, ref Bitmap result_bm, int x, int y, byte ALPHA) { 
    System.Drawing.Color clr; 

    int py, px; 
    for (py = 0; py <= watermark_bm.Height - 1; py++) { 
     for (px = 0; px <= watermark_bm.Width - 1; px++) { 
      clr = watermark_bm.GetPixel(px, py); 
      if (clr.A != 0 || clr.R != 0 || clr.G != 0 || clr.B != 0) 
       watermark_bm.SetPixel(px, py, System.Drawing.Color.FromArgb(ALPHA, clr.R, clr.G, clr.B)); 
     } 
    } 
    Graphics gr = Graphics.FromImage(result_bm); 
    gr.DrawImage(watermark_bm, x, y); 
} 
+0

您還需要顯示'DrawWatermark'的代碼。 – leppie 2010-08-11 08:21:27

+0

@我剛剛發佈每當我收到帶有創建的jpg文件的電子郵件時,其尺寸越來越大,並且上面有相同的第一個文本... – theklc 2010-08-11 08:32:49

+0

@看看代碼,我添加了方法DrawWatermark – theklc 2010-08-11 08:51:27

回答

0

我會看看你是如何創建位圖。

位圖bmp =(位圖)Bitmap.FromFile(imageDir);

這條線是否需要每次都在那裏,即imagedir一直都是一樣的?

你在創建它後處理生成的位圖嗎?

什麼在這個函數中發生:

DrawWatermark(REF waterbmp,裁判BMP,LeftIndex,TopIndex,ALPHA);

你似乎然後保存bmp文件,但不處理它?

我已經看到了一些來自GDI +的奇怪行爲,所以我會從這裏開始。

+0

@查看代碼,我添加了DrawWatermark的方法 – theklc 2010-08-11 08:50:19

+0

catch(異常ex)LogNotificationState(「K」,0,-1,2,false,ex,tableCode); } bmp.Dispose(); bmp = null; 它不起作用! – theklc 2010-08-11 08:53:41

+0

@我找到了答案,我把MemoryStream ms = new MemoryStream(); 在循環中,所以現在它工作得很好! 謝謝 – theklc 2010-08-11 09:04:55