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?