0

I have winform project in C#. I want to export the drawing to pdf file. The drawing is on image file.

The method I use for this is to transfer the color codes and locations of all the pixels in the picture to the datatable.

DataTable dt;

private void CreateDataTable()
{
   dt = new DataTable();
   dt.Columns.Add("No");
   dt.Columns.Add("Lx");
   dt.Columns.Add("Ly");
   dt.Columns.Add("HexCode");
 }
 private void GetThePixelAndLocation()
 {
   Bitmap img = new Bitmap("ProfileImages\\BF74-F02.JPG");
   for (int i = 0; i < img.Width; i++)
   {
     for (int j = 0; j < img.Height; j++)
     {
       Color pixel = img.GetPixel(i, j);
       dt.Rows.Add();
       dt.Rows[dt.Rows.Count - 1]["No"] = dt.Rows.Count;
       dt.Rows[dt.Rows.Count - 1]["Lx"] = i;
       dt.Rows[dt.Rows.Count - 1]["Ly"] = j;
       dt.Rows[dt.Rows.Count - 1]["HexCode"] = pixel.Name; ;   
     }
   }
 }

Then I transfer the drawing on the pdf file with this pixel and location information. For creating pdf file i used PdfSharp nugget.

public void CreatePdfDocumentFromImage(List<myPbox> myPbox)
{
    CreateDataTable();
    GetThePixelAndLocation();
    PdfDocument document = new PdfDocument();
    PdfPage page = document.AddPage();
    page.Width = 1200;
    page.Height = 800;
    //page.Size = new PdfSharp.PageSize();
    XGraphics gfx = XGraphics.FromPdfPage(page);

    for (int i = 0; i < dt.Rows.Count-1; i++)
    {
        string HexCode = dt.Rows[i]["HexCode"].ToString();
        if (!HexCode.Equals("ffffffff"))
        {
            int x1 = Convert.ToInt32(dt.Rows[i]["Lx"]);
            int y1 = Convert.ToInt32(dt.Rows[i]["Ly"]);
            int x2 = Convert.ToInt32(dt.Rows[i + 1]["Lx"]);
            int y2 = Convert.ToInt32(dt.Rows[i + 1]["Ly"]);
            DrawLine(gfx, x1, x2, y1, y2, HexCode);
        }        
    }    
    const string filename = "Unicode_tempfile.pdf";
    // Save the document...
    document.Save(filename);
    Process.Start(filename);
}
void DrawLine(XGraphics gfx, int x1, int x2, int y1, int y2,string HexCode)
{
    XPen pen = new XPen(XColor.FromName(HexCode), 1);
    gfx.DrawLine(pen, x1, y1, x2, y2);
}

It worked, but the result is very bad as you see in the picture below.. How can I improve the quality of the resulting drawing?

enter image description here

Gokhan
  • 453
  • 4
  • 10
  • 2
    I don't know this `PdfSharp` thing. However, as [these answers](https://stackoverflow.com/questions/18854935/overlay-image-onto-pdf-using-pdfsharp) suggest, create `XImage` from the `BF74-F02.JPG`, create graphics from new `PdfPage`, then use it to draw that `XImage`. Makes sense. – dr.null Aug 21 '22 at 21:09
  • 2
    _!HexCode.Equals("ffffffff")_ don't expect this to be true when you would hope it would; allow some slackl! – TaW Aug 21 '22 at 22:07
  • 1
    @dr.null many thanx. I ll use to draw image. – Gokhan Aug 22 '22 at 10:03
  • Consider using an imaging toolkit like LEADTOOLS which allows conversion of images directly to PDF in one line using this code: [`RasterCodecs.Convert("BF74-F02.JPG", "Output.pdf", RasterImageFormat.RasPdfJpeg411, 0, 0, 24, null)`](https://www.leadtools.com/help/sdk/v22/dh/co/rastercodecs-convert(string,string,rasterimageformat,int,int,int,codecsimageinfo).html). (Disclaimer: I am employee of the vendor). If you’d like to try it, there’s a [free evaluation here](https://leadtools.com/downloads) – Hussam Barouqa Aug 25 '22 at 15:18

0 Answers0