2015-07-01 48 views
-1

目前我正在學習使用圖形方法繪製條形碼並將其配置爲位圖。但是,當我要保存它時,雖然它可以成功保存,但結果是黑色圖像,從中看不到任何東西。發生了什麼問題?如何將圖形轉換爲位圖並將其保存在c#中?

private void buttonDraw_Click(object sender, EventArgs e) 
    { 
     System.Drawing.Graphics g = this.pictureBoxBarcode.CreateGraphics(); 
     g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.SystemColors.Control), 
     new Rectangle(0, 0, pictureBoxBarcode.Width, pictureBoxBarcode.Height)); 
     ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0)); 
     g.Dispose(); 

    } 

這是我保存功能

public void Save(Bitmap original) 
    { 
     // Displays a SaveFileDialog so the user can save the Image 
     // assigned to Button2. 
     SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
     saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|Png Image|*.png"; 
     saveFileDialog1.Title = "Save an Image File"; 
     saveFileDialog1.ShowDialog(); 

     // If the file name is not an empty string open it for saving. 
     if (saveFileDialog1.FileName != "") 
     { 
      // Saves the Image via a FileStream created by the OpenFile method. 
      System.IO.FileStream fs = 
       (System.IO.FileStream)saveFileDialog1.OpenFile(); 
      // Saves the Image in the appropriate ImageFormat based upon the 
      // File type selected in the dialog box. 
      // NOTE that the FilterIndex property is one-based. 

      switch (saveFileDialog1.FilterIndex) 
      { 
       case 1: 
        original.Save(fs, 
         System.Drawing.Imaging.ImageFormat.Jpeg); 
        break; 

       case 2: 
        original.Save(fs, 
         System.Drawing.Imaging.ImageFormat.Bmp); 
        break; 

       case 3: 
        original.Save(fs, 
         System.Drawing.Imaging.ImageFormat.Gif); 
        break; 

       case 4: 
        original.Save(fs, 
         System.Drawing.Imaging.ImageFormat.Png); 
        break; 
      } 
      fs.Close(); 
     } 
    } 

我不能提供任何圖片,證明我的名聲是不夠的,這樣做的。抱歉。

+1

我懷疑有人可以在不看一些代碼的情況下回答問題。 – Wazaaaap

+0

看看:https://stackoverflow.com/questions/8316987/background-turns-black-when-saving-bitmap-c-sharp – SteveFerg

+0

@SteveFerg我很好奇這是什麼畫區?畫布,位圖或其他東西? –

回答

0

要繪製成Bitmap使用此代碼:

private void buttonDraw_Click(object sender, EventArgs e) 
{ 
    Bitmap bmp = new Bitmap(pictureBoxBarcode.ClientSize.Width, 
          pictureBoxBarcode.ClientSize.Height); 
    using (Graphics g = Graphics.FromImage(bmp)) 
    { 
     g.Clear(SystemColors.Control); 
     ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0)); 
    } 
    // display: 
    pictureBoxBarcode.Image = bmp; 
    // save: 
    bmp.Save(yourfilename, ImageFormat.Png); 
    // ..or..: 
    Save(bmp); 
} 

或者:

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    Save((Bitmap) pictureBoxBarcode.Image); 
} 

我抄你的繪製代碼。

我不知道爲什麼你想讓它看起來灰色..?

我假設ean13.DrawEan13Barcode是使用函數傳入的Graphics對象繪製的東西到與該Graphics對象關聯的Bitmap

+0

ty爲你的答案,通過這段代碼後,我發現我以前的錯誤是我用圖形做的,而不是位圖的權利? –

+0

什麼是灰色的外觀? –

+0

1:你的代碼是混合。請注意,您始終使用Graphics對象,並始終將其綁定到Btimap。如果您在控件上繪圖,則會在Paint事件中創建Graphics對象,並且Bitmap是控件的表面。在這裏,我們繪製一個位圖並自己創建Graphics對象,並可以在Image屬性中顯示位圖。 - 2:'SystemColors.Control'在大多數機器上是一種灰色, – TaW

相關問題