【问题标题】:Picturebox image to pdf document is not collected well windows formPicturebox图像到pdf文档没有收集好windows窗体
【发布时间】:2021-07-19 04:25:28
【问题描述】:

我有一个图片框可以在它上面绘制,没有任何图像,在白色背景上。那幅画,我想保存为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,如果您想使用位图。我很确定winforms默认使用bmp(位图),可能是转换错误?
  • 使用 PictureBox 的 Paint 事件将 Bezier 曲线添加到 GraphicsPath。当您在 PictureBox 上完成绘图(用作预览)时,使用与在 PictureBox 上绘图相同的过程将结果保存到位图。这里有一个例子:How to draw a signature and save it to disc as a Bitmap?。这是一种略有不同的语言。如果你有兴趣,我可以翻译。顺便说一句,您需要在绘制形状之前指定SmoothingMode = SmoothingMode.AntiAlias
  • @SimpleCoder 有没有试过的解决方案,我改了pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);对于 Png 而不是 Jepg,现在它显示它而不是白色背景。如果你愿意,请给出答案,我接受它

标签: c# winforms itext windows-forms-designer itext7


【解决方案1】:

正如您所说的对此进行分析,请使用 Png Jpg 或 Bmp 来执行此操作。 你说它解决了你的问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多