位图属性,当Bitmap是自定义类的属性时,抛出ArgumentException - "参数无效"

时间:2014-09-04 15:39:10

标签: c# bitmap

我有两节课。

public class Photo
{
    string url;
    Bitmap image;

    public string URL
    {
        get { return this.url; }
        set { this.url = value; }
    }

    public Bitmap Image
    {
        get { return this.image; }
        set { this.image = value; }
    }
}

public partial class Person
{
    private List<Photo> photos;

    public List<Photo> Photos
    {
        get { return this.photos; }
        set { this.photos = value; }
    }

    // In this method I populate a Photo object with a Bitmap and then I add the 
    // Photo object to a collection of Photo objects. The Bitmap is coming from a 
    // web page that only has one image in it, nothing else, not even text. The 
    // web stuff works perfectly well.
    public string Fill( /* various parameters */ )
    {
        .
        .
        .
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            MemoryStream ms = new MemoryStream();
            using (Stream responseStream = request.GetResponse().GetResponseStream())
            {
                byte[] buffer = new byte[1024];
                int bytes;
                while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, bytes);
                }
            }
            Photo photo = new Photo();
            photo.Image = new Bitmap(Image.FromStream(ms));
            this.Photos.Add(photo);
        }
        .
        .
        .
    }
}

当我遍历Photos集合并获得ArgumentException时出现错误,&#34;参数无效&#34;对于所有p.Image属性,例如下面显示的宽度。

int totalWidth = 0;
foreach (Photo p in person.Photos)
{
    totalWidth += p.Image.Width;
}

我已经测试了进入Person.Image的Bitmap,它没问题。该错误给出了Bitmap不喜欢被另一个类封装的外观 - 当然这是荒谬的。我无法弄清楚为什么会这样。

0 个答案:

没有答案