如何将位图添加到ImageList

时间:2014-03-11 14:56:20

标签: c# imagelist

我在winforms中使用此代码将多个图像合并为一个图像:

private Bitmap CombineImages(params Image[] images)
{
    int width = 0;
    for (int i = 0; i < images.Length; i++)
        width += images[i].Width + 3;

    int height = 0;
    for (int i = 0; i < images.Length; i++)
    {
        if (images[i].Height > height)
            height = images[i].Height;
    }

    int nIndex = 0;
    using (Bitmap fullImage = new Bitmap(width, height))
    {
        using (Graphics g = Graphics.FromImage(fullImage))
        {
            g.Clear(SystemColors.AppWorkspace);
            foreach (Image img in images)
            {
                using (img)
                {
                    if (nIndex == 0)
                    {
                        g.DrawImage(img, new Point(0, 0));
                        nIndex++;
                        width = img.Width;
                    }
                    else
                    {
                        g.DrawImage(img, new Point(width, 0));
                        width += img.Width;
                    }
                }
            }
        }

        return fullImage;
    }

    //img3.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
    //img3.Dispose();
    //imageLocation.Image = Image.FromFile(finalImage);
}

当我尝试将此BitMap添加到选项卡控件图像列表时,会出现以下错误:

customerTabCtrl.ImageList.Images.Add("DealingAndBloomberg",     CombineImages(global::Client.CustomerInformation.Properties.Resources.reuters, global::Client.CustomerInformation.Properties.Resources.bloomberg));

System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.ImageList.CreateBitmap(Original original, Boolean& ownsBitmap)
at System.Windows.Forms.ImageList.ImageCollection.Add(Original original, ImageInfo imageInfo)
at System.Windows.Forms.ImageList.ImageCollection.Add(String key, Image i   mage)
at Client.CustomerInformation.FrmCustomerInformationMain.AddCustomer(Int32 callLogId, Boolean bringToFront, Int32 interactionType) in C:\Projects\TradeCentricClient\ClientInformation\FrmCustomerInformationMain.cs:line 378

谁能告诉我这里有什么问题? BitMap继承自Image,那么该错误是什么?

1 个答案:

答案 0 :(得分:0)

问题是您在“using”语句中隐式处理图像对象,因此在添加图像列表时它们不可用。

如果需要,请不要丢弃图像,一旦完成后丢弃整个ImageList,它将自行处理包含的图像。