Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

printing a form in C# as a graphic

Hello all, I found this code but it doesnt work to well - I am trying to print a form in a winform app using WPF as a graphic. This is in the formname.xaml.cs

protected void btnPrint_Click(object sender, EventArgs e)
{
	PrintDocument pd = new PrintDocument();
	pd.PrintPage += new PrintPageEventHandler(PrintImage);
	pd.Print();      
}

void PrintImage(object o, PrintPageEventArgs e)
{
	int x = SystemInformation.WorkingArea.X;
	int y = SystemInformation.WorkingArea.Y;
	int width = this.Width;
	int height = this.Height; 

	Rectangle bounds = new Rectangle(x, y, width, height); 

	Bitmap img = new Bitmap(width, height); 

	this.DrawToBitmap(img, bounds);
	Point p = new Point(100, 100);
	e.Graphics.DrawImage(img, p);    
 } 

Open in new window

any and all help is aprecciated
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Are you trying to get the whole form?...or just the client area?
Avatar of r3nder

ASKER

The whole form and i get errors on
int width = this.Width;// seems to want a double
int height = this.Height;// seems to want a double
and
Bitmap img = new Bitmap(width, height); //wants more signatures
Should be:
            Bitmap img = new Bitmap(this.Size.Width, this.Size.Height);
            this.DrawToBitmap(img, new Rectangle(0, 0, img.Width, img.Height));
            Point p = new Point(100, 100);
            e.Graphics.DrawImage(img, p);    

Open in new window

Avatar of r3nder

ASKER

Still getting errors,
       protected void btnPrint_Click(object sender, EventArgs e)
        {
            
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(PrintImage);
            pd.Print();
        }

        void PrintImage(object o, PrintPageEventArgs e)
        {
            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            int width = SystemInformation.WorkingArea.Width;
            int height = SystemInformation.WorkingArea.Height;

            System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(x, y, width, height);

            Bitmap img = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height));
            Point p = new Point(100, 100);
            e.Graphics.DrawImage(img, p);    
        }

        private void DrawToBitmap(Bitmap img, System.Windows.Shapes.Rectangle rectangle)
        {
            throw new NotImplementedException();

Open in new window


see the attachment
Capture.PNG
Avatar of r3nder

ASKER

down to one error - any idea?
Capture.PNG
Your code:

           Bitmap img = new Bitmap(this.Width, this.Height)

My code:

           Bitmap img = new Bitmap(this.Size.Width, this.Size.Height);
Avatar of r3nder

ASKER

Does not contain a definition for Size

           
            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            //int width = 1920;
            //int height = 768;
            Bitmap img = new Bitmap(1920, 768);
            System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0, 0, img.Width, img.Height);
            DrawToBitmap(img, bounds);
            System.Drawing.Point p = new System.Drawing.Point(0,0);
            e.Graphics.DrawImage(img, p);
        }

        private void DrawToBitmap(Bitmap img, System.Windows.Shapes.Rectangle bounds)
        {
            img = new Bitmap(img, 1920, 768);
            //throw new NotImplementedException();
        }

   
        private void DrawToBitmap(Bitmap img, System.Drawing.Rectangle bounds)
        {
            img = new Bitmap(img, 1920, 768);
            //throw new NotImplementedException();
        }
Got it to go to the printer but nothing is on the  paper
R
Sorry...WPF is confusing me here.  =)

Have you tried just casting the doubles to ints?

    Bitmap img = new Bitmap((int)this.Width, (int)this.Height)
Avatar of r3nder

ASKER

sorry parameters not valid -  Iam at my wits end:)
I am not sure if it can be useful for you, but this is a method I use to get the print-screen of the whole screen (you may need to specify window size and absolute position):

private static void CaptureScreen(String FileName)
        {
            try
            {
                using (var screenBmp = new Bitmap((int)SystemParameters.PrimaryScreenWidth,
                                                    (int)SystemParameters.PrimaryScreenHeight,
                                                        System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    using (var bmpGraphics = Graphics.FromImage(screenBmp))
                    {
                        bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
                        BitmapSource bmpSource = Imaging.CreateBitmapSourceFromHBitmap(
                        screenBmp.GetHbitmap(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());

                        BitmapEncoder bmpEncoder = null;
                        if (Path.GetExtension(FileName) == "jpg")
                            bmpEncoder = new JpegBitmapEncoder() { QualityLevel = 20 };
                        else
                            bmpEncoder = new PngBitmapEncoder();

                        bmpEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
                        using (FileStream fs = new FileStream(FileName, FileMode.Create))
                            bmpEncoder.Save(fs);
                    }
                }
            }
            catch
            {
            }
        }

Open in new window


Regards.
Avatar of r3nder

ASKER

It still is not working - if anyone has another Idea on how to print this _ I could really use the help  - maybe combine the 2 bitmap capture and printing in WPF
ASKER CERTIFIED SOLUTION
Avatar of r3nder
r3nder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is interesting. Could you please show us how your code works to do this?
Thank you!
Avatar of r3nder

ASKER

here is the C#
      private void PrintClick(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();
            if (dialog.ShowDialog() == true)
            { dialog.PrintVisual(_PrintCanvas, "Quality Control Inspection Instructions"); }
        }

Open in new window


here is canvas tags
  <Canvas x:Name="_PrintCanvas">
        
        <Grid Width="816" MinHeight="500" VerticalAlignment="Top" Margin="0,0,0,0" >
        <Border BorderBrush="Silver" BorderThickness="1" Style="{StaticResource DropShadowStyle}" Background="{StaticResource InfoBackgroundBrush}">
            <StackPanel Orientation="Vertical" Margin="48">
<!-- ALL YOUR CODE HERE FOR THE FORM-->

                            </Grid>
                    <Button Content="Click Here to Print" Width="Auto" Padding="5" Margin="10" HorizontalAlignment="Left" Click="PrintClick" ></Button>
                </StackPanel>
        </Border>
    </Grid>
        </Canvas>
</UserControl>

Open in new window


See - that simple
Avatar of r3nder

ASKER

figured it out myself - thanks for the help though