我希望将我的表格打印出来,但是印出来的图像非常模糊。实际上,它非常模糊,以至于我无法阅读它。
首先,我捕捉我的表格:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
internal void CaptureScreen()
{
Application.DoEvents();
Graphics mygraphics = frm.CreateGraphics();
Size s = frm.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, frm.ClientRectangle.Width, frm.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
这里的屏幕图像非常清晰和干净。
这是OnPrintPage方法:
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
Bitmap img;
int width = e.MarginBounds.Width;
int height = e.MarginBounds.Height;
if (memoryImage.Size.Width > width || memoryImage.Size.Height > height)
{
img = ResizeImage(memoryImage, new Size(width, height));
}
else
{
img = memoryImage;
}
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawImage(img, e.MarginBounds.Top, e.MarginBounds.Left);
}
调整大小方法:
private static Bitmap ResizeImage(Bitmap imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return b;
}
我知道调整图片大小会使其模糊。但是在打印页面上绘制它会使其更加模糊。
有没有什么技巧可以让图像变得更清晰?我做错了什么吗?
谢谢大家。