Picturebox图片转pdf文档不好采集windows形式

Picturebox image to pdf document is not collected well windows form

我有一个 pictureBox 可以在它上面绘制,没有任何图像,在白色背景上。那幅画,我想保存为pdf(图片)

对于图片框,我有以下代码:

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

            }

        }
    }

为了创建 pdf,我使用 itext7,在内存中创建,我使用的代码是:

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

因此,我总是得到一个黑色背景的图像,而不是我实际写的。

正如您所说的要回答这个问题,请使用 Png Jpg 或 Bmp 来完成此操作。 你说它解决了你的问题。

您可以进一步研究 pdf 中的转换或图像使用。