简体   繁体   English

如何在PrintPage事件中将Graphics对象转换为Bitmap对象?

[英]How do I convert a Graphics object to a Bitmap object, inside PrintPage event?

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). 我有一个第三方绘图/制图(IoComp绘图),并且我想在另一个第三方报告程序包(Combit List&Label)创建的报告中嵌入一个高质量 (至少600 dpi)绘图控件的位图。

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). 在尝试了许多其他方法之后,我认为我唯一可以做的就是创建一个Plot控件的隐藏实例,所有实例都按比例缩放到打印机大小(大约是屏幕大小的5倍)。 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. 我可以从绘图控件的PrintPage事件中获得具有适当分辨率的Graphics对象,但是将其转换为Bitmap以便报表包满意是事实,这是主要的绊脚石。 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. 我发现的唯一有希望的线索建议使用一种Bitmap构造函数,该构造函数将Graphics实例作为参数。

However, it's not working for me. 但是,它对我不起作用。 It creates Bitmap, but with no content from the Plot control - it is a pure black image. 它创建位图,但没有Plot控件的内容-它是纯黑色图像。

Here's my code (edited to show drawing of red line to Graphics object): 这是我的代码(已编辑以显示到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();
}

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 ! 据我确定(我花了很多时间看) ,没有办法直接从Plot控件中获得高分辨率,打印质量的位图

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. 我需要将Graphics对象转换为Bitmap对象。

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). (例如,我可以从Plot控件中获取低质量 (72 bpi)的位图,并考虑过尝试对其进行拉伸-但我从未见过这种方法在其他应用程序中能很好地起作用)。

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. 但这对位图没有影响-它仍然是纯黑色。

However, it's not working for me. 但是,它对我不起作用。 It creates Bitmap, but with no content from the Plot control - it is a pure black image. 它创建位图,但没有Plot控件的内容-它是纯黑色图像。

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. 您假设在该事件中为输出进行了实际绘制。

The constructor you're using does not copy any graphics, only sets the resolution equal to the graphics resolution. 您使用的构造函数不会复制任何图形,只会将分辨率设置为等于图形分辨率。 See msdn . 参见msdn As Leppie points out, you have to actually draw something to the bitmap. 正如Leppie指出的那样,您实际上必须在位图上绘制一些东西。 I suggest getting another graphics object for the item you just created. 我建议为刚创建的项目获取另一个图形对象。

Graphics g = Graphics.FromImage(bmp);
//Make the background white
g.FillRectangle(Brushes.White, 0, 0, 1000, 1000);

It is not easy to control the DPI in the Print(Preview) system. 在打印(预览)系统中控制DPI并不容易。

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. 不需要PrintDocument。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM