Printing a winform C# application - c#

I created an application which is larger in size than an A4 paper . I placed a print button on my windows form and following the MSDN tried both printDocument and printForm . But both of them don't scale my image and print only half the screen .
I researched and found that somewhere it was advised to place all controls in a picturebox and print that , but I am looking for a better alternative before I go ahead changing the complete design of my application . Some places advised to go for a completely different route for crystal reports .
The task doesn't seem easy than I actually thought and now I am stuck .
Edit1 :Here is the code that I added to my Form . I did try using bitmaps but that did not resolve the problem too .
private void button1_Click(object sender, EventArgs e)
{
//Form3 dlg = new Form3();
//dlg.ShowDialog();
//CaptureScreen();
// printDocument2.Print();
printForm1.Print();
}
/* Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument2_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void groupBox4_Enter(object sender, EventArgs e)
{
}*/
Is there a way if I can also possibly change the orientation to landscape ? I would like to try to see of that probably fits .
Edit2 : I found a way to print in the landscape mode and all the contents fit well . My application has a windows form and 3 tabs in it under a tab control . What I am trying to print is only the contents of the third tab which is the result window . Is there a way I can only print the contents of the tab control and not the entire form ?

Related

How can I print everything from a panel in a windows form C#?

I have an issue with printing from a panel (C#, windows form). I created a Print button, I wrote the necessary codes for that and it seems that only one textboxes are able to shown as it is supposed to be. The rest of the controls, such as the richtextboxes are blank, even if I write into them.
Bitmap bitmap;
private void btn_Print_Click(object sender, EventArgs e)
{
Print(this.panel_Doc_Print);
}
public void Print(Panel panel)
{
PrinterSettings set = new PrinterSettings();
panel_Doc_Print = panel;
PrintArea(panel);
print.Document = print_doc;
print_doc.PrintPage += new PrintPageEventHandler(PrintImage);
print.ShowDialog();
}
public void PrintImage(object sender, PrintPageEventArgs e)
{
Rectangle page_area = e.PageBounds;
e.Graphics.DrawImage(bitmap, (page_area.Width / 2) - (this.panel_Doc_Print.Width / 2), this.panel_Doc_Print.Location.Y);
}
public void PrintArea(Panel panel)
{
bitmap = new Bitmap(panel.Width, panel.Height);
panel.DrawToBitmap(bitmap, new Rectangle(0, 0, panel.Width, panel.Height));
}
Before the printing the Windows form looks like this:
Windows form before printing.
Before I click on the print button:
before
After I click on the print button the following happens:
after
What did I do wrong? When I click on the button I want the program to print the whole panel, including all items and control in it. It happens only to the textboxes. As you can see on the 3rd picture the size of the printing image is quite small. I want it to roughly fill out an A4 sheet of paper.
At this point I am too confused to solve this problem alone, therefore I would like to ask for your help.

How can I freeze the video displayed in the CameraControl?

How can I freeze the video displayed in the CameraControl when TakeSnapshot() has been called in order to display the fetched image?
Basically, I would like to rebuild the same capture behaviour as in the devexpress TakePictureDialog class in my own form, because in the TakePictureDialog it does not seem to be possible to store the user-selected camera device, which I need to do in my app, though.
I followed the instructions and examples in these articles:
https://www.devexpress.com/Support/Center/Question/Details/T269133/obtaining-the-last-selected-camera-device-and-restoring-it-in-a-takepicturedialog
https://documentation.devexpress.com/WindowsForms/114582/Controls-and-Libraries/Editors-and-Simple-Controls/Camera-Control
UserAppDataReigistry not persisting
Welcome to the stackoverflow.
CameraControl doesn't provide that functionality itself.
Either way you can use the Paint event to render the image, obtained via TakeSnapshot.
Something like this:
private void CameraControl1_Paint(object sender, PaintEventArgs e)
{
CameraControl c = sender as CameraControl;
if (isStopped && img != null)
e.Graphics.DrawImage(img, new Rectangle(0,0, c.Width, c.Height));
}
bool isStopped = false;
Bitmap img;
private void button1_Click(object sender, EventArgs e)
{
img = cameraControl1.TakeSnapshot();
cameraControl1.Stop();
isStopped = true;
}
Credits from devexpress.com

Why undo Drawline() in my opinion is not work?

I try to remove newest drawline.
declaration
Bitmap DrawArea ; // global variable
Bitmap Previuos_DrawArea; // global variable
when I click button to draw a line
private void button2_Click_1(object sender, EventArgs e)
{
Graphics g = Graphics.FromImage(DrawArea);
Previuos_DrawArea_img = DrawArea;
g.(new Pen(Brushes.BlueViolet, 1.0F),0,10,10,20);
pictureBox1.Image = DrawArea;
}
when I click button to remove a line
private void button3_Click_1(object sender, EventArgs e)
{
pictureBox1.Image = Previuos_DrawArea_img;
}
Concept :
1st step - Declare variable.
2nd step - Backup current picture.
3rd step - Draw new picture.
4th step - if undo just draw the backup picture.
You aren’t creating a copy of the bitmap, you’re only storing it in two variables. They point to the same bitmap so editing one affects the other one. You’ll need to create a copy:
Previuos_DrawArea_img = new Bitmap(DrawArea);
Now it is a separate image and whatever you do to one of them doesn’t affect the other one.

How to do two different graphics plotting in parallel using C#

I am developing the GUI in C# using picturebox, button, and check box.I need to plot two different plots in parallel using one text file on an image. The image first reads in a picture box. The rest of the program has to be executed in the following way:
Reads text file contains the data points which have to be plotted.
After clicking the button the program execution started with the plotting of the First graphic (rectangular dot), however when the check box is checked the second graphic (continuous dots--path plot) plotting started in parallel with the first graphic.
when the check box is unchecked the second graphic stopped plotting and disappears.(As Both of the graphics style using the same text file).
I need help what to do in this case, should I create the separate thread for check box for this parallel plotting??
please help me where I am mistaken? And pardon my horrible English
From my point of view, the easiest way would be to reload the image into the picture box again and then redraw the first graphics object on it. So, the second one 'disappears'.
An additional thread makes no sense, because drawing must occur on the UI thread only (Windows general rule for GDI+, WinForms, WPF).
Such basic drawing as in your example is very fast.
Edit:
namespace PictureBoxDrawing
{
public partial class Form1 : Form
{
private Bitmap _bmpImage;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_bmpImage = new Bitmap(#"C:\Image.jpg");
InitializePictureBox(_bmpImage);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
DrawPictureBox(pictureBox1, 10, 10, checkBox1.Checked);
}
private void button1_Click(object sender, EventArgs e)
{
DrawPictureBox(pictureBox1, 10, 10, checkBox1.Checked);
}
private void button2_Click(object sender, EventArgs e)
{
InitializePictureBox(_bmpImage);
}
private void DrawPictureBox(PictureBox pb, int x, int y, bool drawBlue)
{
using (Graphics g = pb.CreateGraphics())
{
g.FillRectangle(Brushes.Red, x,y, 9,9);
if(drawBlue)
g.FillRectangle(Brushes.Blue, x, y, 7, 7);
}
}
private void InitializePictureBox(Bitmap bmp)
{
pictureBox1.Image = bmp;
}
}
}
From your example, here is my simplified suggestion. Load and cache the bitmap in a field for later use. I have added a second button2, which can be used to reload the image into the picture box to demonstrate the behaviour. Because the red rectangle is greater than the blue (9 > 7), it overwrites it when redrawing. So it is not necessary to reload the bitmap, if the position is constant. If the position changes, call InitializePictureBox prior to the DrawPictureBox call.

Allow user to copy image from picturebox and save it everywhere

In my application I have a pictureBox that shows an image. When user right clicks on the pictureBox and selects Copy from the context menu, I want to copy the image into the clipboard so the user can paste it in folders and anywhere else. How can I do that?
EDIT: I use this code but by this user only can paste image into word.
var img = Image.FromFile(pnlContent_Picture_PictureBox.ImageLocation);
Clipboard.SetImage(img);
Clipboard.SetImage copies the image content (binary data) to the clipboard not the file path. To paste a file in Windows Explorer you need to have file paths collection in the clipboard not their content.
You can simply add the path of that image file to a StringCollection and then call the SetFileDropList method of Clipboard to achieve what you want.
System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
FileCollection.Add(pnlContent_Picture_PictureBox.ImageLocation);
Clipboard.SetFileDropList(FileCollection);
Now user can past the file anywhere e.g. Windows Explorer.
More info on Clipboard.SetFileDropList Method http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setfiledroplist.aspx
This is the solution when the picture box does not display a file image, but it is rendered upon with GDI+.
public partial class Form1 : Form
{
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// call render function
RenderGraphics(e.Graphics, pictureBox1.ClientRectangle);
}
private void pictureBox1_Resize(object sender, EventArgs e)
{
// refresh drawing on resize
pictureBox1.Refresh();
}
private void copyToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
// create a memory image with the size taken from the picturebox dimensions
RectangleF client=new RectangleF(
0, 0, pictureBox1.Width, pictureBox1.Height);
Image img=new Bitmap((int)client.Width, (int)client.Height);
// create a graphics target from image and draw on the image
Graphics g=Graphics.FromImage(img);
RenderGraphics(g, client);
// copy image to clipboard.
Clipboard.SetImage(img);
}
private void RenderGraphics(Graphics g, RectangleF client)
{
g.SmoothingMode=SmoothingMode.AntiAlias;
// draw code goes here
}
}

Categories

Resources