Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - How to draw to a bitmap and printer?

 
 
Old 02-21-2006   #1 (permalink)
fö


 
 

How to draw to a bitmap and printer?

I am working on a graphing module which plots xy-data, e.g.
a fever curve, in a 2D chart. In the old times of GDI the low
level drawing function got a CDC and a target rectangle:

Chart.Draw(CDC* pDC, CRect TargetRect)

With this approach I was able to draw to a bitmap and a
printer without any effort. pDC just pointed to a memory DC
or to a printer DC.

Now I want to do the same with WPF. The on-screen drawing
is now done by a function which gets a canvas and a rectangle:

Chart.Draw(Canvas canvas, Rect targetRect)

When drawing to a bitmap or printer I do not only want to copy
the window content to the bitmap, but I also want to give the
image a new width or height, i.e. the target rectangle changes.
So the discussion about "Extracting an image file from the
content of a window" does not seem to help. Or does it?

Any help appreciated.

My System SpecsSystem Spec
Old 02-21-2006   #2 (permalink)
Jason Dolinger


 
 

Re: How to draw to a bitmap and printer?

fö wrote:
> I am working on a graphing module which plots xy-data, e.g.
> a fever curve, in a 2D chart. In the old times of GDI the low
> level drawing function got a CDC and a target rectangle:
>
> Chart.Draw(CDC* pDC, CRect TargetRect)
>
> With this approach I was able to draw to a bitmap and a
> printer without any effort. pDC just pointed to a memory DC
> or to a printer DC.
>
> Now I want to do the same with WPF. The on-screen drawing
> is now done by a function which gets a canvas and a rectangle:
>
> Chart.Draw(Canvas canvas, Rect targetRect)
>
> When drawing to a bitmap or printer I do not only want to copy
> the window content to the bitmap, but I also want to give the
> image a new width or height, i.e. the target rectangle changes.
> So the discussion about "Extracting an image file from the
> content of a window" does not seem to help. Or does it?
>
> Any help appreciated.


Hey,

The RenderTargetBitmap class does support a Height and Width method so
maybe you could try setting these before actually saving the bitmap to
the file. It would look something like this (given your graph extends
from Visual and has the name foVisual).

RenderTargetBitmap bmp = new RenderTargetBitmap((int)foVisual.ActualWidth,

(int)foVisual.ActualHeight,
96, //
standard WPF ppi setting
96,

PixelFormats.Pbgra32);


bmp.Render(foVisual);

// Now try setting the Width and Length of bmp here...

// write the bitmap to a file
using (FileStream fs = new FileStream(@"C:\test.png", FileMode.Create))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmp));
enc.Save(fs);
}


Give this a shot.
My System SpecsSystem Spec
Old 02-21-2006   #3 (permalink)
fö


 
 

Re: How to draw to a bitmap and printer?

Hi Jason,
that works fine!

Thanx,
fö

"Jason Dolinger" wrote:

> Jason Dolinger wrote:
> > fö wrote:
> >
> >> I am working on a graphing module which plots xy-data, e.g. a fever
> >> curve, in a 2D chart. In the old times of GDI the low level drawing
> >> function got a CDC and a target rectangle:
> >> Chart.Draw(CDC* pDC, CRect TargetRect)
> >>
> >> With this approach I was able to draw to a bitmap and a printer
> >> without any effort. pDC just pointed to a memory DC or to a printer DC.
> >> Now I want to do the same with WPF. The on-screen drawing is now done
> >> by a function which gets a canvas and a rectangle:
> >> Chart.Draw(Canvas canvas, Rect targetRect)
> >>
> >> When drawing to a bitmap or printer I do not only want to copy the
> >> window content to the bitmap, but I also want to give the image a new
> >> width or height, i.e. the target rectangle changes. So the discussion
> >> about "Extracting an image file from the content of a window" does not
> >> seem to help. Or does it?
> >> Any help appreciated.

> >
> >
> > Hey,
> >
> > The RenderTargetBitmap class does support a Height and Width method so
> > maybe you could try setting these before actually saving the bitmap to
> > the file. It would look something like this (given your graph extends
> > from Visual and has the name foVisual).
> >
> > RenderTargetBitmap bmp = new RenderTargetBitmap((int)foVisual.ActualWidth,
> >
> > (int)foVisual.ActualHeight,
> > 96, //
> > standard WPF ppi setting
> > 96,
> >
> > PixelFormats.Pbgra32);
> >
> >
> > bmp.Render(foVisual);
> >
> > // Now try setting the Width and Length of bmp here...
> >
> > // write the bitmap to a file
> > using (FileStream fs = new FileStream(@"C:\test.png", FileMode.Create))
> > {
> > PngBitmapEncoder enc = new PngBitmapEncoder();
> > enc.Frames.Add(BitmapFrame.Create(bmp));
> > enc.Save(fs);
> > }
> >
> >
> > Give this a shot.

>
> Sorry for the poor code formatting there, I'm not sure why it's all
> screwed up... That's how it cut and pasted from VS...
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Corel draw x13 Software
Draw Pad Issues Windows Live
Merge one bitmap onto another in vb .NET General
bitmap Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46