taj*_*i01 14 .net c# printing picturebox winforms
我正在使用WinForms.在我的表格中,我有一个用于显示图像文档的图片框.问题是当我裁剪图像然后打印文档时图像变得稍微扭曲.如果我不裁剪图像文档并定期打印,图像文档不会变形.
如何裁剪和打印图像文档而不会使图像文档失真?
或者是否有更好的方法来编码,以便它可以裁剪和打印而不会使图像文档失真?如果是这样,我该怎么办?
笔记:
我的图片框设置为缩放,因为我使用的图像很大:
图像文档示例尺寸:2500 x 3100
我的图片框没有边框
int _cropX, _cropY, _cropWidth, _cropHeight;
public Pen _cropPen;
private State _currentState;
private enum State
{
Crop
}
private void Open_btn_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
try
{
if (Crop_Checkbox.Checked == true)
{
Cursor = Cursors.Default;
if (_currentState == State.Crop)
{
if (_cropWidth < 1)
{
return;
}
Rectangle rect = new Rectangle(_cropX, _cropY, _cropWidth, _cropHeight);
//First we define a rectangle with the help of already calculated points
Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
//Original image
Bitmap img = new Bitmap(_cropWidth, _cropHeight);
// for cropinf image
Graphics g = Graphics.FromImage(img);
// create graphics
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//set image attributes
g.DrawImage(originalImage, 0, 0, rect, GraphicsUnit.Pixel);
pictureBox1.Image = img;
pictureBox1.Width = img.Width;
pictureBox1.Height = img.Height;
}
}
else
{
Cursor = Cursors.Default;
}
}
catch (Exception)
{
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
Cursor = Cursors.Cross;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//X and Y are the coordinates of Crop
pictureBox1.Refresh();
_cropWidth = e.X - _cropX;
_cropHeight = e.Y - _cropY;
pictureBox1.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _cropWidth, _cropHeight);
}
}
}
else
{
Cursor = Cursors.Default;
}
}
private void Crop_Checkbox_CheckedChanged(object sender, EventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
this.Cursor = Cursors.Cross;
}
}
private void Print_btn_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(printDocument1_PrintPage);
myPrinDialog1.Document = myPrintDocument1;
if (myPrinDialog1.ShowDialog() == DialogResult.OK)
{
myPrintDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(pictureBox1.Image, 10, 10); //(Standard paper size is 850 x 1100 or 2550 x 3300 pixels)
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
_cropX = e.X;
_cropY = e.Y;
_cropPen = new Pen(Color.FromArgb(153, 180, 209), 3); //2 is Thickness of line
_cropPen.DashStyle = DashStyle.DashDotDot;
pictureBox1.Refresh();
}
}
}
else
{
Cursor = Cursors.Default;
}
}
Run Code Online (Sandbox Code Playgroud)测试:略有失真:
测试:未扭曲:
测试: 上面的图片是一个测试.这是当我从pictureBox1_MouseUp中取出以下代码时发生的事情:
Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
Run Code Online (Sandbox Code Playgroud)
并编辑/替换(originalImage to pictureBox1.Image):
g.DrawImage(pictureBox1.Image, 0, 0, rect, GraphicsUnit.Pixel);
Run Code Online (Sandbox Code Playgroud)
Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
Run Code Online (Sandbox Code Playgroud)
这很可能就是问题开始的地方。这可能会导致 pictureBox1.Image 重新缩放以强制适合 pictureBox1 大小。取决于图片框是否有边框及其 SizeMode 属性值。重新缩放会导致图像重新采样,新位图中像素的颜色是根据所选插值模式的指示,根据原始图像中其相邻像素的值计算的。
这实际上模糊了生成的图像。这在照片上效果很好,但这种文本在很大程度上取决于抗锯齿像素,才能在低分辨率显示器上看起来不错。对这些像素的轻微更改会破坏效果,并且它们不再将字母形状与背景平滑地混合。它们变得更加明显,最好的描述方式是生成的文本看起来“胖”。
我在发布的代码中根本看不到这样做的明显理由。删除该语句并将originalImage替换为pictureBox1.Image
.
另请注意,打印此图像可能会令人失望。现在,这些抗锯齿像素变成了纸上 6x6 的墨水斑点。只有当你有长手臂时,这才看起来不错。只要字体这么小,并且您无法控制抗锯齿选项,那么您就无能为力。仅当您使用 PrintDocument 和 Graphics.DrawString() 时,打印文本才会看起来不错。
归档时间: |
|
查看次数: |
855 次 |
最近记录: |