r3nder
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);
}
any and all help is aprecciated
Are you trying to get the whole form?...or just the client area?
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
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);
ASKER
Still getting errors,
see the attachment
Capture.PNG
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();
see the attachment
Capture.PNG
ASKER
down to one error - any idea?
Capture.PNG
Capture.PNG
Your code:
Bitmap img = new Bitmap(this.Width, this.Height)
My code:
Bitmap img = new Bitmap(this.Size.Width, this.Size.Height);
Bitmap img = new Bitmap(this.Width, this.Height)
My code:
Bitmap img = new Bitmap(this.Size.Width, this.Size.Height);
ASKER
Does not contain a definition for Size
int x = SystemInformation.WorkingA rea.X;
int y = SystemInformation.WorkingA rea.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.Rect angle 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
int x = SystemInformation.WorkingA
int y = SystemInformation.WorkingA
//int width = 1920;
//int height = 768;
Bitmap img = new Bitmap(1920, 768);
System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0
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.Rect
{
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)
Have you tried just casting the doubles to ints?
Bitmap img = new Bitmap((int)this.Width, (int)this.Height)
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):
Regards.
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
{
}
}
Regards.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is interesting. Could you please show us how your code works to do this?
Thank you!
Thank you!
ASKER
here is the C#
here is canvas tags
See - that simple
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"); }
}
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>
See - that simple
ASKER
figured it out myself - thanks for the help though