I am working on a project where I want to save records as a Jpeg in a tabular format of 2 rows and 4 columns in A4 size. The format contains few labels and a textbox which will get their data from a database. What I think is to add a Main panel which contains around 8 child panels in form of 2 rows and 4 columns which will get populated with data from a database. But I don't know how to save the main panel as a jpeg in A4 size in a folder.
答案 0 :(得分:0)
1st Alternative: Copy Form bitmap to file
You could do something like the following:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
....
// This code saves the current Form Control.
// In order to save some other Control,
// replace "this" by the Control object
Control ctl = this;
Bitmap bm = new Bitmap(ctl.Bounds.Width, ctl.Bounds.Height);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(ctl.Left, ctl.Top, 0, 0, bm.Size);
g.Dispose();
bm.Save("bitmap.jpg", ImageFormat.Jpeg);
....
Note that the resulting picture won't have the nice rounded corners of your form. Some consmetics are needed to delete the screen background surrounding the corners. Due to the translucent nature of the form, parts of the screen content are visible. Add some error checking and calculate the desired filename.
2nd Alternative: Draw Form Control to bitmap
As suggested by @Alexander in his comment:
Control ctl = this;
Rectangle rectangle = ctl.Bounds;
Bitmap bm = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(bm);
Rectangle bmRectangle = new Rectangle(0, 0, bm.Width, bm.Height);
ctl.DrawToBitmap(bm, bmRectangle);
g.Dispose();
bm.Save("bitmap.jpg", ImageFormat.Jpeg);
This variant avoids problems with visible parts of the screen background. Recursive calls don't appear to be necessary.
3rd Alternative: Create HTML and use external rendering tool
You could create a HTML
text file from your C#
application and call an external tool to convert HTML
to JPG
.
The rendering of HTML
to JPG
can be accomplished using wkhtmltoimage:
set IN=yyy.html
set OUT=xxx.jpg
set HEIGHT=640
wkhtmltoimage.exe --height %HEIGHT% %IN% %OUT%
The printing size A4 is a matter of scaling.