从 picturebox C# 打印高质量的 Tiff 文档
Print high quality Tiff documents from picturebox C#
我正在使用 WinForms。我的程序将 .Tiff 图像文件打开到一个图片框中。我遇到的问题是从图片框中打印高质量的 tiff 图像。我已经多次尝试和测试打印。当文档打印出来时,文字不是 crisp/clear,有点模糊。我还测试了我的打印机以检查我的打印机是否有问题。我用 Microsoft word 打印了一个普通的文本文档,它打印得很清楚,所以我的代码有问题。
有人可以提供代码以在我的图片框中打印高质量的 .tiff 图像吗?我是否需要以编程方式增加我的 DPI 以提高图像质量?
这是我的代码。
private void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.DrawImage(pictureBox1.Image, 25, 25, 800, 1050);
}
private void Print_button_Click(object sender, EventArgs e)
{
PrintPreviewDialog.Document = PrintDocument;
PrintPreviewDialog.ShowDialog();
}
进一步分析我的代码后,我想我明白了为什么我的照片变得模糊了。当我打开一个 Tiff 文档时,我的表单中有一个用于多个 tiff 页面的后退和前进按钮。在 RefreshImage 方法中,我认为这是我的图像变得模糊的地方。
这是我的代码:
private int intCurrPage = 0; // defining the current page (its some sort of a counter)
bool opened = false; // if an image was opened
//-------------------------------------Next and Back Button-------------------------------------------------
private void btn_Back_Click(object sender, EventArgs e)
{
if (opened) // the button works if the file is opened. you could go with button.enabled
{
if (intCurrPage == 0) // it stops here if you reached the bottom, the first page of the tiff
{ intCurrPage = 0; }
else
{
intCurrPage--; // if its not the first page, then go to the previous page
RefreshImage(); // refresh the image on the selected page
}
}
}
private void btn_Next_Click(object sender, EventArgs e)
{
if (opened) // the button works if the file is opened. you could go with button.enabled
{
if (intCurrPage == Convert.ToInt32(lblNumPages.Text)) // if you have reached the last page it ends here
// the "-1" should be there for normalizing the number of pages
{ intCurrPage = Convert.ToInt32(lblNumPages.Text); }
else
{
intCurrPage++;
RefreshImage();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
var bm = new Bitmap(pictureBox1.Image);
bm.SetResolution(600, 600);
Image image1 = new Bitmap(bm);
pictureBox1.Image = image1;
pictureBox1.Refresh();
}
public void RefreshImage()
{
Image myImg; // setting the selected tiff
Image myBmp; // a new occurance of Image for viewing
myImg = System.Drawing.Image.FromFile(@lblFile.Text); // setting the image from a file
int intPages = myImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page); // getting the number of pages of this tiff
intPages--; // the first page is 0 so we must correct the number of pages to -1
lblNumPages.Text = Convert.ToString(intPages); // showing the number of pages
lblCurrPage.Text = Convert.ToString(intCurrPage); // showing the number of page on which we're on
myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage); // going to the selected page
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height); // setting the new page as an image
// Description on Bitmap(SOURCE, X,Y)
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
尝试将 PictureBox 的上下文转换为位图图像并让您选择可能在哪个设备上进行打印的代码:
private void myPrintDocument_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);
myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));
e.Graphics.DrawImage(myBitmap1, 0, 0);
myBitmap1.Dispose();
}
private void btnPrintPicture_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
PrintDialog myPrinDialog1 = new PrintDialog();
myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);
myPrinDialog1.Document = myPrintDocument1;
if (myPrinDialog1.ShowDialog() == DialogResult.OK)
{
myPrintDocument1.Print();
}
}
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp;
别再看了,您正在重新缩放图像以适合图片框。这会丢弃源图像中的大量像素,图片框比图像小很多。纵横比只是偶然正确的顺便说一句。
最简单的方法就是不自己重新缩放,而让 PictureBox 控件来完成。尽管这会使绘画变慢。另一种方法是简单地保留对原始图像的引用。只需使用变量而不是 PictureBox.Image 属性.
我正在使用 WinForms。我的程序将 .Tiff 图像文件打开到一个图片框中。我遇到的问题是从图片框中打印高质量的 tiff 图像。我已经多次尝试和测试打印。当文档打印出来时,文字不是 crisp/clear,有点模糊。我还测试了我的打印机以检查我的打印机是否有问题。我用 Microsoft word 打印了一个普通的文本文档,它打印得很清楚,所以我的代码有问题。
有人可以提供代码以在我的图片框中打印高质量的 .tiff 图像吗?我是否需要以编程方式增加我的 DPI 以提高图像质量?
这是我的代码。
private void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.DrawImage(pictureBox1.Image, 25, 25, 800, 1050);
}
private void Print_button_Click(object sender, EventArgs e)
{
PrintPreviewDialog.Document = PrintDocument;
PrintPreviewDialog.ShowDialog();
}
进一步分析我的代码后,我想我明白了为什么我的照片变得模糊了。当我打开一个 Tiff 文档时,我的表单中有一个用于多个 tiff 页面的后退和前进按钮。在 RefreshImage 方法中,我认为这是我的图像变得模糊的地方。 这是我的代码:
private int intCurrPage = 0; // defining the current page (its some sort of a counter)
bool opened = false; // if an image was opened
//-------------------------------------Next and Back Button-------------------------------------------------
private void btn_Back_Click(object sender, EventArgs e)
{
if (opened) // the button works if the file is opened. you could go with button.enabled
{
if (intCurrPage == 0) // it stops here if you reached the bottom, the first page of the tiff
{ intCurrPage = 0; }
else
{
intCurrPage--; // if its not the first page, then go to the previous page
RefreshImage(); // refresh the image on the selected page
}
}
}
private void btn_Next_Click(object sender, EventArgs e)
{
if (opened) // the button works if the file is opened. you could go with button.enabled
{
if (intCurrPage == Convert.ToInt32(lblNumPages.Text)) // if you have reached the last page it ends here
// the "-1" should be there for normalizing the number of pages
{ intCurrPage = Convert.ToInt32(lblNumPages.Text); }
else
{
intCurrPage++;
RefreshImage();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
var bm = new Bitmap(pictureBox1.Image);
bm.SetResolution(600, 600);
Image image1 = new Bitmap(bm);
pictureBox1.Image = image1;
pictureBox1.Refresh();
}
public void RefreshImage()
{
Image myImg; // setting the selected tiff
Image myBmp; // a new occurance of Image for viewing
myImg = System.Drawing.Image.FromFile(@lblFile.Text); // setting the image from a file
int intPages = myImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page); // getting the number of pages of this tiff
intPages--; // the first page is 0 so we must correct the number of pages to -1
lblNumPages.Text = Convert.ToString(intPages); // showing the number of pages
lblCurrPage.Text = Convert.ToString(intCurrPage); // showing the number of page on which we're on
myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage); // going to the selected page
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height); // setting the new page as an image
// Description on Bitmap(SOURCE, X,Y)
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
尝试将 PictureBox 的上下文转换为位图图像并让您选择可能在哪个设备上进行打印的代码:
private void myPrintDocument_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);
myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));
e.Graphics.DrawImage(myBitmap1, 0, 0);
myBitmap1.Dispose();
}
private void btnPrintPicture_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
PrintDialog myPrinDialog1 = new PrintDialog();
myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);
myPrinDialog1.Document = myPrintDocument1;
if (myPrinDialog1.ShowDialog() == DialogResult.OK)
{
myPrintDocument1.Print();
}
}
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp;
别再看了,您正在重新缩放图像以适合图片框。这会丢弃源图像中的大量像素,图片框比图像小很多。纵横比只是偶然正确的顺便说一句。
最简单的方法就是不自己重新缩放,而让 PictureBox 控件来完成。尽管这会使绘画变慢。另一种方法是简单地保留对原始图像的引用。只需使用变量而不是 PictureBox.Image 属性.