Pergunta

I am using GDI+ to draw the initial image on PictureBox - which renders a clean image. I am trying to then capture that drawing and draw it on a PDF using PDFSharp which works, but comes out blurry. I am sure it has something to do with the fact I have changed the destination Rectangle's size. What do i need to do to clean it up?

Code:

Dim bmp As New Bitmap(pb.Width, pb.Height)
Dim pdf As New PdfDocument
Dim page As PdfPage = pdf.AddPage
Dim g As XGraphics = XGraphics.FromPdfPage(page)
pb.DrawToBitmap(bmp, New Rectangle(pb.ClientRectangle.X, pb.ClientRectangle.Y, pb.Width, pb.Height + 20))
g.SmoothingMode = XSmoothingMode.AntiAlias
g.DrawImage(bmp, New XRect(New RectangleF(20, 0, 600, 800)))
pdf.Save(_path) 
Foi útil?

Solução

It's the anti-aliasing that makes images blurry. AFAIK Adobe Reader draws images with anti-aliasing when used with PDFsharp.

With PDFsharp create an XImage from your BMP and then set

image.Interpolate = false;

for that XImage. This will give a hint to Adobe Reader that anti-aliasing is not wanted for that image.

With respect to screen shots, anti-aliasing is useful when scaling down (e.g. when taking a 400x300 bitmap from an 800x600 screen), but not when scaling up (like Adobe Reader will do with images embedded in PDF files).

See also:
http://forum.pdfsharp.net/viewtopic.php?p=5370#p5370

Outras dicas

If you scale an image up, it will come out blurred. There is no other way since there are no additional information in the image and it will just be interpolated in some way. There is no "Zoom in and ENHANCE"-button. :-)

What I did in one of my programs where I wanted to save a controls current look to a file, was to first scale the control to the desired size, then draw it to the bitmap, then resize it down again.

If this works depends on the content of the control of course, wether it's scalable or not and so on.

e.g.

In the example below I have a Chart control that I work with in my export dialog called workingChart. The steps that are used are:

  • Save old size
  • Resize chart to the desired size
  • Draw control to bitmap
  • Resize chart back to the old size

This works well and the image comes out crisp, since you do not resize the image itself.

Private Function GetChartScaledImage(wantsize As Size) As Bitmap
    Dim oldsize As Size = workingChart.Size
    Dim bmp As New Bitmap(wantsize.Width, wantsize.Height)
    workingChart.Size = wantsize
    workingChart.DrawToBitmap(bmp, New Rectangle(0, 0, wantsize.Width, wantsize.Height))
    workingChart.Size = oldsize
    Return bmp
End Function
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top