English 中文(简体)
如何确定参数,在衣着和处理来源比图之后,不是有效的例外。
原标题:How to fix Parameter is not valid exception after Cloning and Disposing source Bitmap
  • 时间:2012-04-16 15:49:25
  •  标签:
  • c#

我有两个班子:

public class ImageHandler
{
    private Bitmap _currentBitmap;
    private Bitmap _bitmapbeforeProcessing;

    public Bitmap CurrentBitmap
    {
        get
        {
            if (_currentBitmap == null)
            {
                _currentBitmap = new Bitmap(1, 1);
            }
            return _currentBitmap;
        }
        set { _currentBitmap = value; }
    }

    public string CurrentBitmapPath { get; set; }

    public void ResetBitmap()
    {
        if (_currentBitmap != null && _bitmapbeforeProcessing != null)
        {
             Bitmap temp = (Bitmap)_currentBitmap.Clone();
            _currentBitmap = (Bitmap)_bitmapbeforeProcessing.Clone();
            _bitmapbeforeProcessing = (Bitmap)temp.Clone();
        }
    }

    internal void RestorePrevious()
    {
        _bitmapbeforeProcessing = _currentBitmap;
    }
 }

而且:

public class RotationHandler
{
    private ImageHandler imageHandler;

    public void Flip(RotateFlipType rotateFlipType)
    {
        this.imageHandler.RestorePrevious();
        Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
        this.imageHandler.CurrentBitmap.Dispose(); // dispose of current bitmap
        bitmap.RotateFlip(rotateFlipType);
        this.imageHandler.CurrentBitmap = bitmap;
   }
 }

在轮换后打电话到<代码>ResetBitmap()时,显示:

参数无效

但是,如果:

this.imageHandler.CurrentBitmap.Dispose();

之后,它会进行罚款。 但是,如果将<代码>Flip(>)方法称为“记忆例外”。

我如何解决这一问题?

问题回答

虽然比特图是一个C#客体,但它实际上是一个胜诉的32个标物,因此,当你与它一道行事时,你必须称“处置”。

你正在这样做:

_CurrentBitmap = _CurrentBitmap.Clone();

当你做时:

_Temp = _CurrentBitmap.Clone();
_CurrentBitmap.Dispose();
_CurrentBitmap = _Temp;

我只是处理同样的问题。

电话:Clone( on a Bitmap 物体只生成一个浅薄拷贝;它仍将参照同一基本图像数据,因此,其中一物体的<代码>Dispose(<>/code>将删除原始和复印数据。 电话:RotateFlip(> 待处理Bitmap 反对结果如下:

系统:参数无效

解决这一问题的途径之一是建立一个新的<代码>。 Bimap Object based on the original one, than using Clone (. 这将复制图像数据和管理物体数据。

例如,而不是:

Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();

使用:

Bitmap bitmap = new Bitmap(this.imageHandler.CurrentBitmap);

请注意,如果源码标的无效,这也将导致<代码>ArgumentException,因此,一般来说,下列内容较好(I ve 更名为的源比图>>。

Bitmap bitmap = (currentBitmap == null) ? null : new Bitmap(currentBitmap)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...