Domanda

I Have this code in winforms to combine multiple images into one image:

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);
}

When I try to add this BitMap to a tab control image list it gives the following error:

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

Can anyone please tell me what is wrong here ??? BitMap inherites from Image, so what is that error ?

È stato utile?

Soluzione

The problem is that you are implicitly disposing the image objects in the "using" statement, so they are not available for the image list when you add them.

Don't dispose the images, if necessary, dispose the whole ImageList once your done and it will dispose the images contained in by itself.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top