Avatar of r3nder
r3nder
Flag 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
.NET ProgrammingProgrammingC#

Avatar of undefined
Last Comment
r3nder

8/22/2022 - Mon
Mike Tomlinson

Are you trying to get the whole form?...or just the client area?
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
Mike Tomlinson

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
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
r3nder

ASKER
down to one error - any idea?
Capture.PNG
Mike Tomlinson

Your code:

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

My code:

           Bitmap img = new Bitmap(this.Size.Width, this.Size.Height);
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
Mike Tomlinson

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)
r3nder

ASKER
sorry parameters not valid -  Iam at my wits end:)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jonnidip

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.
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
r3nder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jonnidip

It is interesting. Could you please show us how your code works to do this?
Thank you!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
r3nder

ASKER
figured it out myself - thanks for the help though