Results 1 to 10 of 10

Thread: [RESOLVED] Use Transparent Colors When Printing to PDF

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [RESOLVED] Use Transparent Colors When Printing to PDF

    Hello,

    I'm having a problem using transparent colors within the graphics object of the System.Drawing.Printing.PrintDocument class. If I create two overlapping rectangles with the alpha channel of the color set to some value of transparency, the output colors are the same as setting the alpha component to not transparent, so the rectangle that is drawn second is on top of the first rectangle, completely covering that portion of the rectangle. I've tried to get transparencies to work, but I can't. The closest I've come is to set,
    Code:
    e.Graphics.CompositingMode = CompositingMode.SourceOver
    This works, but the printout is extremely pixelated and quite ugly.

    In the end, I want to be able to draw various shapes, and then overlay a transparent GIF image. The GIF is already transparent (this is done using any third party image editing program that supports transparencies), and I am 100% certain that the GIF that I'm using as an overlay is transparent.

    Also, I have what I want to do working in the program within a picture box. (I know picture boxes are not really meant for drawing shapes and such, but I'm editing someone else's project, so we're stuck with a picturebox.)

    I really don't understand why this isn't working.

    Thanks.

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Use Transparent Colors When Printing to PDF

    Hi arcanine,
    it sounds to me like you are blowing up a small image to print it. The quality of an enlarged image depends on the Graphics InerpoltionMode. The default (misleadingly named InterpolationMode.None) is a compromise between pixellation and blurring; it's main merit is speed but it's not very pretty. You can get real pixellation by choosing InterpolationMode.NearestNeighbour or a higher class of blurring with InterpolationMode.HighQualityBicubic.

    But do you really need to blow up the image so much? Couln't you do your image compositing in a suitably large bitmap. Then the size of the image doesn't depend on the size of the PictureBox. You could work out the relation between the bitmap size and the print size from the Bitmap's Resolution property, which is by default 120*120 dots per inch (on my PC at least). So if you want to print an image at 6 x 8 inches at 1:1 on paper, you would need a bitmap of 720 * 960 pixels. Here's how you could code it:

    Code:
    Dim bmp As New Bitmap(720, 960)
    Using g As Graphics = Graphics.FromImage(bmp)
       g.CompositingMode = CompositingMode.SourceOver
       g.InterpolationMode = InterpolationMode.HighQualityBicubic
       g.DrawImage ... 'and do all your imaging work here.
       '....
    End Using
    PictureBox1.Image = bmp
    If you set the PictureBox.SizeMode to Zoom or Stretch, you won't have to worry about its actual size relative to the printed image.

    Apart from that, there's nothing wrong with using PictureBoxes to draw images. They have the advantage of being DoubleBuffered by default, which is important to avoid flickering if the image changes at runtime. But if you are concerned about print size or resolution, treat the PictureBox as a "viewer" rather than a "container" of the image.

    BB

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Use Transparent Colors When Printing to PDF

    Where does the "Printing to PDF" part come into your question? I don't see anything related to pdf yet
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Use Transparent Colors When Printing to PDF

    Quote Originally Posted by stanav View Post
    Where does the "Printing to PDF" part come into your question? I don't see anything related to pdf yet
    I assumed "print to PDF" is defined as a virtual printer. BB

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Use Transparent Colors When Printing

    it sounds to me like you are blowing up a small image to print it.
    Possibly, but the image I'm using is 1008x1008. I've already looked into the quality settings within the graphics class, and have set everything to the best quality.

    Attached are samples of what I'm talking about. The file "NoOverlay.jpg" shows all of the shapes that I'm drawing. None of the colors of the shapes include transparencies. "Overlay.gif" is the transparent GIF that I want to draw on top of the shapes. In the program, the transparent parts let the shapes show through, but when I print, the result is shown in "Overlay-SourceCopy.jpg". The transparency is not shown. Then when I set the following:

    Code:
    e.Graphics.CompositingMode = CompositingMode.SourceOver
    I get the result in "Overlay-SourceOver.jpg". The GIF is transparent, but horribly pixelated.

    Also, I know the GIF is a different size than the JPG images, but the program already accounts for this, and draws everything with the correct ratios and positions. I don't think the size difference has anything to do with it.

    Where does the "Printing to PDF" part come into your question? I don't see anything related to pdf yet
    That's true, I haven't mentioned that. I guess the only reason that it's relevant, is because I've been printing to PDF and it hasn't been working. I just tried printing to a physical printer, and the same outcome occurred as printing to a PDF. I changed the reply title.

    I assumed "print to PDF" is defined as a virtual printer.
    I'm not sure. The code uses the PrintDocument class.
    Attached Images Attached Images     

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Use Transparent Colors When Printing to PDF

    The reason I was asking about the term "pdf" is because I thought that you're creating a pdf file. But now it seems obvious that you're not creating pdf file directly - by saying "directly", I mean you produce the pdf in code using some kind of pdf library like iTextSharp, pdfBox...
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Use Transparent Colors When Printing to PDF

    The gif overlay is poor quality and looks bad on anything but a light background.
    I've redone it as a png (attached) which should work better.
    Attached Images Attached Images  
    W o t . S i g

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Use Transparent Colors When Printing to PDF

    The gif overlay is poor quality and looks bad on anything but a light background.
    Well, I feel like an idiot. My apologies to boops boops, as you were pretty much correct about having a small (or low quality) GIF.

    Also, thanks to Milk for spending the time to make a png, and pointing out the low quality GIF again.

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [RESOLVED] Use Transparent Colors When Printing to PDF

    Looking at the pictures, it looks like it is not entirely due to scaling up a small image. It think the black lines in the GIF image have been anti-aliased into an opaque white background, and the resulting off-white pixels are showing up as scruffy lines against the coloured parts of the new background.

    If so, you could clean up the offending image in two ways. Firstly you could use a paint program (e.g. Paint.Net) to select out the pure black pixels and then use a feather tool them to give a bit of transparent anti-aliasing. Alternatively you could do it in code: for example, use a ColorMatrix to transform the white component (the lightness) of each pixel into a transparency level.

    BB

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: [RESOLVED] Use Transparent Colors When Printing to PDF

    I'll look into that. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width