将图形保存到位图之外的位图? [英] Saving Graphics to Bitmap outside of paint?

查看:128
本文介绍了将图形保存到位图之外的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将C#Winforms图形对象保存为位图,但我在Paint事件之外获得了此代码的ArgumentException:

  public Bitmap Bitmap 
{
get
{
return new Bitmap(100,100,this.Graphics);




$ b $ p
$ b

其中this.Graphics刚刚在Paint事件中设置处理程序,我猜对象在事件之外变得无效,这很烦人,因为我想避免为了保存图像而在其中放置代码。任何人都可以指向正确的方向吗?



编辑:
我将不得不重新考虑我的绘画代码,因为我需要能够将控件绘制成位图。

解决方案

是的,在Graphics对象被处理后。没有什么理由使用这个构造函数,它只设置位图分辨率。如果这对你来说真的很重要,那么直接使用Bitmap.SetResolution()方法即可。


Trying to save a C# Winforms Graphics Object to a bitmap, yet I am getting an ArgumentException with this code outside of the Paint event:

public Bitmap Bitmap
{
   get
   {
       return new Bitmap(100, 100, this.Graphics);
   }
}

Where this.Graphics is just set in the Paint event handler, I'm guessing the object becomes invalid outside of the event, which is annoying as I'd like to avoid having to put code in there for saving the images. Can anyone point me in the right direction?

Edit: I will have to re-factor my painting code, as I need to be able to 'draw' the control into a bitmap.

解决方案

Yes, this will bomb after the Graphics object is disposed. There is little reason to use this constructor, it only sets the bitmap resolution. If that's actually important to you then just use the Bitmap.SetResolution() method directly.

这篇关于将图形保存到位图之外的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆