Click here to Skip to main content
15,885,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to save the image name from textboxes using microsoft.office.interop.outlook. I can save the textboxes to the subject line but I need the same name as the image. First it is a .png then converts to a .pdf. Any tips appreciated.
I get error if I remove $ then the name is "
{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.png
"

System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'


Some code I am using.

C#
 private void Convert()
        {
            try
            {
                string filename = Environment.GetFolderPath(
                System.Environment.SpecialFolder.DesktopDirectory) + $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.png";
                string destinaton = Environment.GetFolderPath(
                System.Environment.SpecialFolder.DesktopDirectory) + $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.pdf";
                
                PdfDocument doc = new PdfDocument();
                doc.Pages.Add(new PdfPage() { Size = PdfSharp.PageSize.Letter, Orientation = PdfSharp.PageOrientation.Landscape });
                XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
                XImage img = XImage.FromFile(filename);

                xgr.DrawImage(img, 0, 0);
                doc.Save(destinaton);
                doc.Close();
                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_Analyst_Click(object sender, EventArgs e)
        {
            #region Save Image

            string MyDesktopPath = Environment.GetFolderPath(
            System.Environment.SpecialFolder.DesktopDirectory) + "\\";
                  

            using (Bitmap bmpScreenshot = new Bitmap(Bounds.Width, Bounds.Height, PixelFormat.Format32bppArgb))
            {
                bmpScreenshot.SetResolution(128f, 128f);
                Rectangle rect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
                this.DrawToBitmap(bmpScreenshot, rect);

                // Build the filename
                string myFilepath = Path.Combine(MyDesktopPath, $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.png");
                if (myFilepath == "")
                {
                    Console.WriteLine("Form image save FAILED!");
                    return;
                }
                bmpScreenshot.Save(myFilepath, ImageFormat.Png);
                Console.WriteLine("Form image saved: {0}", myFilepath);
            }

            Convert();
            #endregion
/////////MAIL CODE

<pre> Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem mailItem = outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            
            mailItem.Subject = $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}";  // Add Part Number to Subject line THIS IS WORKING
            mailItem.To = "EmailHere.com";                      

            string imageSrc = Environment.GetFolderPath(
                System.Environment.SpecialFolder.DesktopDirectory) + $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.png"; // Change path as needed


            var attachments = mailItem.Attachments;
            var attachment = attachments.Add(imageSrc);
            attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpg");
            attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"); // Image identifier found in the HTML code right after cid. Can be anything.
            mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true);

            // Set body format to HTML

            mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            string msgHTMLBody = "<html><head></head><body>Hello Analyst,<br><br>This is a working example of embedding an image :<br><br><img align=\"baseline\" border=\"1\" hspace=\"0\" src=\"cid:myident\" width=\"\" 600=\"\" hold=\" /> \"></img><br><br>Regards,<br>My Name</body></html>";
            mailItem.HTMLBody = msgHTMLBody;

            mailItem.Attachments.Add((object)Environment.GetFolderPath(
                System.Environment.SpecialFolder.DesktopDirectory) + $"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.pdf", // Attachment in Mail
            Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)$"{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.pdf");

            mailItem.Send();








What I have tried:

The above code that works with the Subject line
Posted
Updated 7-Jun-21 21:59pm
Comments
[no name] 7-Jun-21 22:42pm    
Display the filename; then consider using Path.Combine().

1 solution

Quote:
I get error if I remove $ then the name is "{txt_CHKR.Text} {txt_PN.Text} {label67.Text}.png"
That's to be expected. With the $ prefix, you have an interpolated string; anything enclosed in {...} braces will be treated as code, and inserted into the final string. Without that prefix, it's just a normal string; what you type is (almost) exactly what you get.

$ - string interpolation - C# reference | Microsoft Docs[^]

The "generic error in GDI+" error could mean anything. Since you've removed the interpolated string prefix, it probably means that the file doesn't exist.

You haven't told us what error you're getting with the $ prefix. If it's telling you that $ is not valid, then you're using an old version of Visual Studio, and you'll need to use string.Format instead:
C#
... + string.Format("{0} {1} {2}.png", txt_CHKR.Text, txt_PN.Text, label67.Text)
String.Format Method (System) | Microsoft Docs[^]

If you're getting a different error, then you need to tell us what the error is.
 
Share this answer
 
Comments
Member 12349103 8-Jun-21 6:52am    
Richard
I am getting that error when trying to create the file this is the code.
error on this line.
bmpScreenshot.Save(myFilepath, ImageFormat.Png);

Thank you

#region Save Image

            string MyDesktopPath = Environment.GetFolderPath(
            System.Environment.SpecialFolder.DesktopDirectory) + "\\";

           // this.Size = new Size((int)(8.5 * 96f), (int)(11 * 96f));

            using (Bitmap bmpScreenshot = new Bitmap(Bounds.Width, Bounds.Height, PixelFormat.Format32bppArgb))
            {
                bmpScreenshot.SetResolution(128f, 128f);
                Rectangle rect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
                this.DrawToBitmap(bmpScreenshot, rect);

                // Build the filename
                string myFilepath = Path.Combine(MyDesktopPath, string.Format("{0} {1} {2}.png", txt_CHKR.Text, txt_PN.Text, label67.Text));
                if (myFilepath == "")
                {
                    Console.WriteLine("Form image save FAILED!");
                    return;
                }
                bmpScreenshot.Save(myFilepath, ImageFormat.Png);
                Console.WriteLine("Form image saved: {0}", myFilepath);
            }

            Convert();
            #endregion
Richard Deeming 8-Jun-21 7:00am    
Well, as I said, "a generic error occurred in GDI+" is a generic error which gets thrown for a wide range of problems. It could be an invalid character in the file name; it could be that you're trying to overwrite an existing file which is locked; it could be a problem with the image itself.

It's a pain to diagnose, but it's not something we can debug for you.
Member 12349103 8-Jun-21 9:30am    
Richard
your String.Format code above works great I will mark as solved.
My generic error was due to a date format in the string 6/8/2021.
I got to work on that.
Thanks for the tip

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900