0

The function below is responsible for getting the screenshot of a panel and using it as an attachment in an email without storing it in the local machine.

I managed to do it but one small problem, it gives me this error:

cannot convert from 'System.Drawing.Bitmap' to 'string'

I know there are similar questions to this but I cant figure out how i can fix this.

private void btnSend_Click(object sender, EventArgs e)
{
    //using (var bmp = new Bitmap(workPanel.Width, workPanel.Height))
    //{
    //  workPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
    //  bmp.Save(@"images/" + name.Text + ".png");
    //}
    try
    {
        using (var bmp = new Bitmap(workPanel.Width, workPanel.Height))
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("email@gmail.com");
            mail.To.Add(txtTo.Text); // Receivers email address comes here
            mail.Subject = txtTitle.Text;
            mail.Body = txtBody.Text;
            mail.IsBodyHtml = true; // TO HAVE THE BODY AS HTML

            System.Net.Mail.Attachment attachment;
            //attachment = new System.Net.Mail.Attachment(lblLocation.Text);
            attachment = new System.Net.Mail.Attachment(bmp); // <-------- ERROR COMES HERE
            mail.Attachments.Add(attachment);

            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            MessageBox.Show("Mail has been sent");
        }
    }
    catch(Exception ex)
    {
        //MessageBox.Show(ex.Message);
        MessageBox.Show("Something went wrong, this might be a problem from our end.\n" + ex.Message, "Please contact support team");
    }
}
Sh.Imran
  • 1,035
  • 7
  • 13
  • Always try MSDN before coming here.. - For anything else than a string you need to include a MediaType in the attachment [constructor](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.attachment.-ctor?view=netcore-3.1) : [mediatypenames](https://learn.microsoft.com/en-us/dotnet/api/system.net.mime.mediatypenames.image?view=netcore-3.1) – TaW Aug 16 '20 at 12:55
  • What would that be in my case – Supreme Gang Certified Member Aug 16 '20 at 12:59
  • 3
    Does this answer your question? [C# Attaching System.Drawing.Image to Email](https://stackoverflow.com/questions/21688300/c-sharp-attaching-system-drawing-image-to-email) – Guru Stron Aug 16 '20 at 12:59
  • 1
    Guru's link contains the answer. https://learn.microsoft.com/pt-br/dotnet/api/system.net.mail.attachment.-ctor?view=netcore-3.1#System_Net_Mail_Attachment__ctor_System_IO_Stream_System_Net_Mime_ContentType_. There is not a constructor that accepts a Bitmap object directly. – Júlio César Schincariol Filho Aug 16 '20 at 13:47

1 Answers1

0

The reason for your error is simple: class Attachment has no constructor that takes a BitMap.

Luckily there is a Constructor that takes a Stream. Alas, BitMap has no method to convert it to a Stream. Luckily BitMap implements ISerializable, so we can Save the BitMap in a stream, and then use the Attachment(Stream, ...) constructor

Let's make Extension methods to convert a BitMap to a Stream, and to convert a Stream to an Attachment. See extension methods demystified

static System.IO.Stream ToStream(this System.Drawing.BitMap bmp)
{
    // TODO: exception if bmp null
    var stream = new MemoryStream();
    Bitmap.Save(ms, Bitmap.RawFormat);
    return stream;
}
static System.Net.Mail.Attachment ToAttachment(this System.Drawing.BitMap bmp)
{
    // TODO: bmp is null

    const string contentType = "image/jpg";
    return new System.Net.Mail.Attachment(bmp.ToStream(), contentType);
}

Usage:

BitMap bmp = ...
Attcahment attachement = bmp.ToAttachment();
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116