[英]Low quality image on print
我有一個圖片框,需要打印。
應用程序將其打印,這是用於打印的代碼:
private void btnPrintSave_Click(object sender, EventArgs e)
{
tempstr = comboBox7.Text;
if (!Directory.Exists(tempstr))
Directory.CreateDirectory(tempstr);
PrintDocument doc = new PrintDocument();
doc.PrintPage += this.Doc_PrintPage;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
temp1 = comboBox1.Text;
temp2 = comboBox2.Text;
temp3 = comboBox3.Text;
temp4 = comboBox4.Text;
temp5 = comboBox5.Text;
temp6 = comboBox6.Text;
bmp.Save(Application.StartupPath + "\\"+ tempstr +"\\" + temp1 + temp2 + temp3 + temp4 + temp5 + temp6 + ".jpg");
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
唯一的問題是圖片非常模糊。 這是最后一張照片 。
有誰知道如何解決這個問題?
(PS最終圖像必須為189(寬度)和132(高度))
您需要明確設置要打印圖像的dpi
。
您的輸入和所需的輸出的縱橫比 不相同。 如果這是您想要的,則需要分別計算寬度和高度的必要dpi
值。
我以一種額外的顯式方式對其進行編碼,以使其最易於理解。
另請注意,您始終應使用ClientSize
或ClientRectangle
屬性,因此可以自由設置任何BorderStyle
,因為Width
是外部寬度。
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
float tgtWidthMM = 50; // 5cm
float tgtHeightMM = 35; // 3.5cm
float tgtWidthInches = tgtWidthMM / 25.4f;
float tgtHeightInches = tgtHeightMM / 25.4f;
float srcWidthPx = bmp.Width; // 633
float srcHeightPx = bmp.Height; // 381
float dpiX = srcWidthPx / tgtWidthInches;
float dpiY = srcHeightPx / tgtHeightInches;
bmp.SetResolution(dpiX, dpiY);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
e.Graphics.DrawImage((Image)bmp, x, y);
}
還要將ImageFormat
參數添加到Save
調用中:
using System.Drawing.Imaging;
...
bmp.Save(Application.StartupPath + "\\"+ tempstr +"\\" + temp1 + temp2 +
temp3 + temp4 + temp5 + temp6 + ".jpg", ImageFormat.Jpeg);
..否則保存成功與否還取決於從jpg
創建的源。
但是,當然,無論您做什么都正確,如果源中的質量不足,結果將無法改變。 再看看您的示例,我想知道圖像的來源是什么?
如果使用Labels
和TextBoxes
創建文本,則您實際上應該通過Print事件中的DrawString
調用來編寫這些文本,因為只有這樣才能產生高質量的輸出。
如果將其加載到PictureBox
我想知道為什么不直接使用源圖像。 另外:如果(絕對)將PictureBox
用作縮放工具,則可能會出現模糊現象。 不要那樣做! 而是使用帶有源矩形和目標矩形的重載僅放大DrawImage
調用! 但是同樣,輸入的質量限制了輸出的質量!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.