如何在 PrintPage 事件中将 Graphics 对象转换为 Bitmap 对象?
这是我试图解决的高级问题...
我有一个第三方绘图/绘图(IoComp Plot),并且我想嵌入一个高质量(至少 600 dpi)位图由另一个第 3 方报告包(Combit List & Label)创建的报告中的 Plot 控件。
这是迄今为止最有希望的方法...
---编辑---:
在尝试了许多其他方法之后,我认为我唯一可以工作的方法是创建一个隐藏实例绘图控件,所有内容都放大到打印机尺寸(大约屏幕尺寸的 5 倍)。这包括宽度和高度、字体大小、线宽——控件的每个可见组件。哎呀!
------
我可以从绘图控件的 PrintPage 事件中获取适当分辨率的 Graphics 对象,但将其转换为位图,以便报表包满意事实证明这是主要的绊脚石。经过几个小时的搜索,其他人也提出了同样的问题,但没有可行的答案。
我发现的唯一有希望的线索建议使用 Bitmap 构造函数之一,该构造函数将 Graphics 实例作为参数。
但是,它对我不起作用。它创建位图,但没有来自绘图控件的内容 - 它是纯黑色图像。
这是我的代码(经过编辑以显示向 Graphics 对象绘制红线):
void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Draw a red line on the Graphics object. When printed, this
// line is shown as part of the normal Plot graphics.
Pen myPen;
myPen = new Pen(Color.Red);
e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
// Create a bitmap from the Graphics object
Bitmap bm = new Bitmap(1000, 1000, e.Graphics);
// Save to disk
// DOES NOT WORK - CREATES FILE THAT IS PURE BLACK (VIEWED
// WITH "PAINT" PROGRAM)
bm.Save(@"C:\Bicw_Dev\Bic.Net\FrontEnd\GraphicsToBmp.bmp", ImageFormat.Bmp);
bm.Dispose();
}
任何人都可以建议为什么这不起作用?这是否是一种有效的方法?
另外,请注意:
据我所知(并且我花了相当多的时间寻找)没有办法直接从绘图控件获得高分辨率、打印质量的位图!
我强调这一点是因为其他提出这个问题的人得到了代码示例作为回应,解决了相反问题 - 将位图转换为图形。
我需要将 Graphics 对象转换为 Bitmap 对象。
如果有人可以建议一种替代方法,让我能够在报告中获得绘图的打印质量图像,请随意。 (例如,我可以从 Plot 控件中获取低质量 (72 bpi) 位图,并考虑尝试拉伸它 - 但我从未见过这种方法在其他应用程序中运行良好)。
谢谢,
-Tom Bushell
编辑回应评论:
作为实验,我添加了以下内容:
Pen myPen;
myPen = new Pen(Color.Red);
e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
这导致当我打印绘图时在绘图图形上绘制一条红线。但它对位图没有影响 - 它仍然是纯黑色。
This is the high level problem I'm trying to solve...
I have a 3rd party plotting/graphing (IoComp Plot), and I want to embed a high quality (at least 600 dpi) bitmap of the Plot control in reports created by another 3rd party report package (Combit List & Label).
This is the approach that seems most promising so far...
---Edit---:
After trying many other approachs, the only one that I think I can make work is to create a hidden instance of the Plot control, with everything scaled up to printer size (approx 5 times screen size). That includes width and height, font sizes, line widths - every visible component of the control. Yuck!
------
I can get a Graphics object of the proper resolution from the plot control's PrintPage event, but converting it to a Bitmap so the report package will be happy is proving to be the major stumbling block. Several hours of searching has led to other people who asked the same question, but no viable answers.
The only promising lead I've found suggested using one of the Bitmap constructors, which takes a Graphics instance as a parameter.
However, it's not working for me. It creates Bitmap, but with no content from the Plot control - it is a pure black image.
Here's my code (edited to show drawing of red line to Graphics object):
void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Draw a red line on the Graphics object. When printed, this
// line is shown as part of the normal Plot graphics.
Pen myPen;
myPen = new Pen(Color.Red);
e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
// Create a bitmap from the Graphics object
Bitmap bm = new Bitmap(1000, 1000, e.Graphics);
// Save to disk
// DOES NOT WORK - CREATES FILE THAT IS PURE BLACK (VIEWED
// WITH "PAINT" PROGRAM)
bm.Save(@"C:\Bicw_Dev\Bic.Net\FrontEnd\GraphicsToBmp.bmp", ImageFormat.Bmp);
bm.Dispose();
}
Can anyone suggest why this doesn't work? Is this even a valid approach?
Also, please note:
As far as I can determine (and I've spent quite a bit of time looking) there is no way to get a high resolution, print quality Bitmap from the Plot control directly!
I stress this because several others who asked the question got code samples in response that solved the opposite problem - converting a Bitmap to a Graphics.
I need to convert a Graphics object to a Bitmap object.
And if anyone can suggest an alternate approach that allows me to get a print quality image of my plots into my reports, please feel free. (For example, I can get a low quality (72 bpi) Bitmap from the Plot control, and have considered trying to stretch it - but I've never seen that approach work well in other applications).
Thanks,
-Tom Bushell
Edit in response to comment:
As an experiment, I added the following:
Pen myPen;
myPen = new Pen(Color.Red);
e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
This caused a red line to be drawn over the plot graphics when I printed my plot. But it had no effect on the Bitmap - it's still pure black.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,你从来不给图形上色,你期望什么?
您应该在该事件中为输出进行实际绘图。
Well you never do paint to the graphics, what do you expect?
You are suppose to do the actual drawing for the output in that event.
您使用的构造函数不会复制任何图形,仅将分辨率设置为等于图形分辨率。请参阅 msdn。正如 Leppie 指出的那样,您必须实际在位图中绘制一些内容。我建议为您刚刚创建的项目获取另一个图形对象。
The constructor you're using does not copy any graphics, only sets the resolution equal to the graphics resolution. See msdn. As Leppie points out, you have to actually draw something to the bitmap. I suggest getting another graphics object for the item you just created.
在打印(预览)系统中控制DPI并不容易。
但也许你的处理方式是错误的。您想要的是创建一个(大)位图,然后“欺骗”控件使用该位图,就像它是屏幕一样。请参阅此链接列表。
不需要打印文档。
It is not easy to control the DPI in the Print(Preview) system.
But maybe you are going about it the wrong way. What you want is to create a (large) bitmap and then 'trick' the control in to using that bitmap as if it was the screen. See this list of links.
No need for PrintDocument.