2017-08-05 40 views
0

我正在製作一個項目,我想將這些記錄保存爲A4格式的2行4列表格格式的Jpeg。 格式包含幾個標籤和一個將從數據庫獲取數據的文本框。我認爲是添加一個主面板,其中包含大約8個子面板,形式爲2行和4列,這些面板將填充來自數據庫的數據。但我不知道如何將主面板保存爲A4文件夾中的jpeg。如何以Jpeg格式導出數據爲A4格式的數據?

+0

我會建議編輯你的問題。據我瞭解,你的主要問題根本與Winforms或數據庫無關,它是關於將2d數組渲染成JPEG文件。 – vorou

+0

您可以通過[Interop Word library](https://www.dotnetperls.com/word)將數據導出到Word,然後將其轉換爲Image,如本頁[建議](https://stackoverflow.com/questions/20326478/convert-word-file-pages-to-jpg-images-using-c-sharp) – saidfagan

+0

參見[DrawToBitmap](https://msdn.microsoft.com/en-us/library/system.windows.forms .control.drawtobitmap(v = vs.110).aspx)方法。遞歸地調用它到你的所有控件。你也可能需要''Bitmap'類的方法,比如'SetResolution'。 –

回答

0

第一備選物:複製表格位圖到文件
你可以做類似如下:

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

.... 

enter image description here

注意,所產生的畫面不會有漂亮的圓角你的形式。 需要一些化妝品來刪除角落周圍的屏幕背景。由於表單的半透明性質,屏幕內容的一部分是可見的。 添加一些錯誤檢查並計算所需的文件名。


第二備選:繪製窗體控件位圖

正如在他的評論中所建議的@Alexander:

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

enter image description here

這種變異避免了的可見部分的問題屏幕背景。 遞歸調用似乎不是必需的。


第三替代方案:創建HTML和使用外部渲染工具

你可以從你的C#應用程序創建一個HTML文本文件,並調用一個external toolHTML轉換爲JPG

HTMLJPG渲染可以使用wkhtmltoimage來完成:

set IN=yyy.html 
set OUT=xxx.jpg 
set HEIGHT=640 

wkhtmltoimage.exe --height %HEIGHT% %IN% %OUT% 

打印尺寸爲A4是縮放的問題。