Search code examples
c#winformsitextwindows-forms-designeritext7

Picturebox image to pdf document is not collected well windows form


I have a pictureBox to draw on top of it, without any images, on a white background. That drawing, I want to save to pdf (as image)

picturebox

For the pictureBox, I have the following code:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {

        lastPoint = e.Location;//we assign the lastPoint to the current mouse position: e.Location ('e' is from the MouseEventArgs passed into the MouseDown event)

        isMouseDown = true;//we set to true because our mouse button is down (clicked)

    }


private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        isMouseDown = false;

        lastPoint = System.Drawing.Point.Empty;

    }


private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {

        if (isMouseDown == true)//check to see if the mouse button is down
        {

            if (lastPoint != null)//if our last point is not null, which in this case we have assigned above
            {

                if (pictureBox1.Image == null)//if no available bitmap exists on the picturebox to draw on
                {

                    //create a new bitmap
                    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

                    pictureBox1.Image = bmp; //assign the picturebox.Image property to the bitmap created

                }

                using (Graphics g = Graphics.FromImage(pictureBox1.Image))
                {//we need to create a Graphics object to draw on the picture box, its our main tool
                    //when making a Pen object, you can just give it color only or give it color and pen size

                    g.DrawLine(new Pen(Color.Black, 2), lastPoint, e.Location);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    //this is to give the drawing a more smoother, less sharper look

                }

                pictureBox1.Invalidate();//refreshes the picturebox

                lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position

            }

        }
    }

For the creation of the pdf, I use itext7, with a creation in memory where the code I use is:

var stream = new MemoryStream();
var writer = new PdfWriter(stream);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);

document.Add(new Paragraph("Hello world!"));


 MemoryStream ms = new MemoryStream();

 pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 byte[] buff = ms.ToArray();
 ImageData rawImage = ImageDataFactory.Create(buff);
 iText.Layout.Element.Image image = new iText.Layout.Element.Image(rawImage);
 document.Add(image);

As a result, I always get an image with a black background, not what I had actually written.

image as document pdf


Solution

  • As you said to put an anwser to this, use Png Jpg or Bmp to do this. You said it solved your question.

    You can research further on conversion or image usage in pdf.