用于Bitmap和正确处理C#的Helper方法

时间:2022-08-16 11:23:43

I have a helper method that creates a bitmap with its own using statement. Originally I had the issue of when returning the object controlled by using statement it would be disposed once scope was left.. So, with the help of this article, I was able to figure out that was my issue as well.

我有一个帮助方法,用自己的using语句创建一个位图。最初我有一个问题,当返回由using语句控制的对象时,一旦范围被遗弃就会处理它。所以,在这篇文章的帮助下,我能够弄清楚这也是我的问题。

So I return a cloned version and pass it to DrawImage of the Graphics class like so,

所以我返回一个克隆版本并将其传递给Graphics类的DrawImage,如下所示,

g.DrawImage(getInputDataImage(dr.DRODAT, oFont, 430, 87, black, white, Color.Transparent), new Point(280, 23)); //DATE

getInputDataImage:

getInputDataImage:

 public static Image getInputDataImage(...
 {
        using(Bitmap bitmap = new Bitmap(width, height))
        using (Graphics graphics = Graphics.FromImage(bitmap))
        using(var format = new StringFormat{Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center})
        {
            //set coordinates on canvas to put barcode
            Point point = new Point(50, 50);

            RectangleF layoutRectangle = new RectangleF(new PointF(0, 0), new SizeF(bitmap.Width, bitmap.Height));
            graphics.Clear(color);
            graphics.DrawString(text, font, fontBrush, layoutRectangle, format);

            return (Bitmap)bitmap.clone();
        }

 }

Will DrawImage call dispose in it's destructor on the classes it uses? How should I handle disposing the Bitmap class when used like this? There is a few cases where I call the getInDataImage helper method with a new object being returned each time. Trying to understand how to maintain disposing this way.

DrawImage会在它使用的类的析构函数中调用dispose吗?我这样使用时如何处理Bitmap类?在某些情况下,我调用getInDataImage辅助方法,每次都返回一个新对象。试图了解如何以这种方式维持处理。

Is it better to return the original object from the helper and not wrap it in a using statement, and handle its disposing in the main method?

从帮助程序返回原始对象并不将其包装在using语句中,并在main方法中处理它的处理是否更好?

1 个解决方案

#1


4  

Try returning the bitmap object itself from the helper method without the "using" statement and without cloning it.

尝试从helper方法返回位图对象本身而不使用“using”语句并且不克隆它。

In your caller, create a new object then dispose:

在您的调用者中,创建一个新对象然后处置:

using(Bitmap b = getInputDataImage(...))
{
    g.DrawImage(b, new Point(...));
    // Do anything else I need to do with "b"
}
// "b" is now disposed of, I can't use it here

When you use the "using", you dispose of the object once the code exits those brackets. So when you create an object, you want it alive while you use it, then you want to dispose of it.

当您使用“使用”时,一旦代码退出这些括号,您将处置该对象。因此,当您创建一个对象时,您希望它在使用时保持活动状态,然后您想要处置它。

The best way to handle this is in the highest level possible. It makes sense to dispose of the Graphics object in your helper method because it never leaves the scope of that method. However, the Bitmap you want DOES leave that method, so you don't want to dispose of it until after it gets used by its caller. So then, logically, the caller would be responsible for disposing of it.

处理此问题的最佳方法是尽可能*别。在helper方法中处理Graphics对象是有意义的,因为它永远不会离开该方法的范围。但是,您想要的位图会离开该方法,因此您不希望在其被调用者使用之后将其丢弃。那么,从逻辑上讲,调用者将负责处理它。

#1


4  

Try returning the bitmap object itself from the helper method without the "using" statement and without cloning it.

尝试从helper方法返回位图对象本身而不使用“using”语句并且不克隆它。

In your caller, create a new object then dispose:

在您的调用者中,创建一个新对象然后处置:

using(Bitmap b = getInputDataImage(...))
{
    g.DrawImage(b, new Point(...));
    // Do anything else I need to do with "b"
}
// "b" is now disposed of, I can't use it here

When you use the "using", you dispose of the object once the code exits those brackets. So when you create an object, you want it alive while you use it, then you want to dispose of it.

当您使用“使用”时,一旦代码退出这些括号,您将处置该对象。因此,当您创建一个对象时,您希望它在使用时保持活动状态,然后您想要处置它。

The best way to handle this is in the highest level possible. It makes sense to dispose of the Graphics object in your helper method because it never leaves the scope of that method. However, the Bitmap you want DOES leave that method, so you don't want to dispose of it until after it gets used by its caller. So then, logically, the caller would be responsible for disposing of it.

处理此问题的最佳方法是尽可能*别。在helper方法中处理Graphics对象是有意义的,因为它永远不会离开该方法的范围。但是,您想要的位图会离开该方法,因此您不希望在其被调用者使用之后将其丢弃。那么,从逻辑上讲,调用者将负责处理它。