Nielsen's Weblog : .NET [use your Context dude]
Updated: 19-11-2005; 09:22:02.

 

Subscribe to "Nielsen's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

20. oktober 2005

100% sure thing, there is a bug in the Graphics.DrawImage function.
Complete disrespect for the ImageAttributes I say :).

I am trying to print a Tiff that supports multi-page documents. This is done by using the
Image.SelectActiveFrame function and Graphics.DrawImage.


SelectActiveFrame selects the current page(or other stuff) into the image and passed on to the DrawImage function.
The DrawImage function should then print what ever image was selected by the SelectActiveFrame..but it doesn't, it simply ignores the imageattributes on the selected image and spits out the very first frame.

the code shown here always prints the first page in the multi-page document,
but it should actually print all pages in the Tiff file.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    bm.SelectActiveFrame(FrameDimension.Page, curPage);  //select current page..
    e.Graphics.DrawImage((Image)bm,0,0); // this always draws the first frame !!!
......

}


this code shown here works, but it is tremendously slow, totally useless if you ask me...

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    ...
    bm.SelectActiveFrame(FrameDimension.Page, curPage); 
    Bitmap tmpBmp = new Bitmap(bm.Width,  bm.Height,System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    using(Graphics g = Graphics.FromImage(tmpBmp)) 
    {
       g.DrawImage((Image)bm,0,0);//render image.. dog slow zzzzzzzzzzzz.
    }
    e.Graphics.DrawImage((Image)tmpBmp,0,0);
    ...
}

so what I came up with and it turns out to be extremly fast, is to save the frame that was selected by
the SelectActiveFrame into a MemoryStream, call FromStream and then draw it:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
 ....
 Image img = Image.FromStream(ms); //ms is a memstream pointing to a tiff picture.
 img.SelectActiveFrame(FrameDimension.Page, curPage);
 using(MemoryStream stm = new MemoryStream())
 {     
  img.Save(stm, imgCodecInfo, encParams); //save to memory stream with Lempel-Ziv-Welch etc.
  Bitmap bmp = (Bitmap)Image.FromStream(stm);
  e.Graphics.DrawImage((Image)bmp,0,0);
  bmp.Dispose();
 }
 img.Dispose();}
....
}

Apparently creating a new image with Image.FromStream makes the DrawImage happy.

"A true friend stabs you in the front."
- Oscar Wilde


1:58:23 PM    comment []

© Copyright 2005 Allan Nielsen.



Click here to visit the Radio UserLand website.
 


October 2005
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Aug   Nov