简体   繁体   中英

released project produces error “The calling thread must be STA, because many UI components require this” when creating bitmap in a DLL

I have a project that when debugging works perfectly fine and has everything correctly checked into TFS. I get the above error message when the released project calls the dll. The main project is in vb too

here is the stack trace stack trace

Error causing line of code at the beginning of the for each loop- Bitmap bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-2.png"));

Most of DLL's code -

    public Dictionary<string, MemoryStream> CreateW2FromBitmap(List<Employee> info, Employer employerInfo, bool fourPerPage)
    {
        var targetPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\" + "W2Images\\"));

        StringFormat rightAlign = new StringFormat(StringFormatFlags.NoClip);
        StringFormat centerAlign = new StringFormat(StringFormatFlags.NoClip);
        PdfPTable imageTable = new PdfPTable(2) { WidthPercentage = 82 };

        imageTable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
        imageTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
        rightAlign.Alignment = StringAlignment.Far;
        centerAlign.Alignment = StringAlignment.Center;

        var brushColor = Brushes.Black;
        var W2ImageDictionary = new Dictionary<string, MemoryStream>();
        var font = new System.Drawing.Font("Courier", 12);
        var document = new Document(PageSize.LETTER, 0, 0, 10, 0);

        foreach (var property in info)
        {
            Bitmap bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-2.png"));
            MemoryStream tempImageStream = new MemoryStream();

            var writer = PdfWriter.GetInstance(document, tempImageStream);
            var employeeCityStateZipCode = CityStateZipDelimiter(property.CityStateZip);
            var employerCityStateZipCode = CityStateZipDelimiter(employerInfo.CityStateZip);

            document.Open();
            writer.CloseStream = false;

            for (int i = 1; i < 5; i++)
            {
                switch (i)
                {
                    case 1:
                        bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-2.png"));
                        break;
                    case 2:
                        bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-2.png"));
                        break;
                    case 3:
                        bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-B.png"));
                        break;
                    case 4:
                        bmp = new Bitmap(Bitmap.FromFile(targetPath + "Copy-B.png"));
                        break;
                }

The fix is changing where the images are stored. They were within the main app's folder. Hope this helps anyone in the future

Bitmap bmp = new Bitmap(Bitmap.FromFile("C:\\\\Copy-2.png));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM