0

currently I am learning to draw barcode using graphics method and dispose it as bitmap. But when I going to save it, although it cans save successfully, but the result is black image, nothing can be seen from that. What is the problem going on?

    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();

    }

And this is my save function

    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();
        }
    }

I cant provide any picture to proof that as my reputation is not enough to do so. Sorry.

Wong Jun Xuan
  • 35
  • 1
  • 7
  • 1
    I doubt that someone could answer the question without watching some code. – aleksandar Jul 01 '15 at 02:05
  • Take a look at: https://stackoverflow.com/questions/8316987/background-turns-black-when-saving-bitmap-c-sharp – SteveFerg Jul 01 '15 at 05:06
  • @SteveFerg I am wonder what is that draw area? Canvas, bitmap or something else? – Wong Jun Xuan Jul 01 '15 at 05:18
  • You have a dozen different choices, take a look at the BitMap class definition: https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx – SteveFerg Jul 01 '15 at 05:46
  • You have two options: Write onto the surface of the PictureBox control using its Paint event and its e.Graphics object. __Or__ you can draw into a Bitmap using a Graphics object you created from it. - Instead you have mixed both ways which will not work! - See [here](http://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=2|0.0381#27341797) for more on those two options..! – TaW Jul 01 '15 at 06:44
  • @TaW the problem is i am just using graphics, i did not even use bitmap – Wong Jun Xuan Jul 01 '15 at 09:06
  • Exactly. That is the problem. You probably want to use a bitmap, since you want to save it. But to clarify: Graphics is __only a tool to draw on the associated bitmaps__. Always. You tried to draw on the surface of the PB, which implicitly is just a bitmap also.. You can ask a control to save its surface with the DrawToBitmap function but you should draw into a Bitmap and assign it to the PB's Image! – TaW Jul 01 '15 at 09:20

1 Answers1

0

To draw into a Bitmap use this code:

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);
}

Or:

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

I have copied your drawing code.

I don't know why you want to make it look gray..?

I am assuming that ean13.DrawEan13Barcode is a function that uses the passed-in Graphics object to draw something onto the Bitmap that is associated with the Graphics object.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • ty for ur answer, after going through this code, i found that my previous mistake is i am doing it with graphics, not bitmap right? – Wong Jun Xuan Jul 02 '15 at 00:25
  • And what is the look gray means? – Wong Jun Xuan Jul 02 '15 at 00:26
  • 1: Your code was a mix. Note that you always use a Graphics object and it is always bound to a Btimap. If you draw on a control, the Graphics object is created in the Paint event and the Bitmap is the surface of the control. Here we draw in a Bitmap and create the Graphics object ourselves and can display the Bitmap in the Image property.. - 2: `SystemColors.Control` is a kind of gray, on most machines, – TaW Jul 02 '15 at 00:35
  • oo, ty for explain, i know where i wrong le, I am just nid to draw a barcode out, so i dun know what is the SystemColors.Control use for, as in the code, you have already cleared it. – Wong Jun Xuan Jul 02 '15 at 03:05