C# Attaching System.Drawing.Image to Email

后端 未结 2 1838
夕颜
夕颜 2021-01-17 22:30

Is there any way to attach System.Drawing.Image to email with out saving it, then grabbing it from the saved path.

Right now I\'m creating the image and saving it. I

2条回答
  •  悲&欢浪女
    2021-01-17 23:21

    In theory, you can convert the Image to a MemoryStream, and then add the stream as an attachment. It would go like this:

    public static Stream ToStream(this Image image, ImageFormat formaw) {
      var stream = new System.IO.MemoryStream();
      image.Save(stream, formaw);
      stream.Position = 0;
      return stream;
    }
    

    Then you can use the following

    var stream = myImage.ToStream(ImageFormat.Gif);
    

    Now that you have the stream, you can add it as an attachment:

    mail.Attachments.Add(new Attachment(stream, "myImage.gif", "image/gif" ));
    

    References:

    System.Drawing.Image to stream C#

    c# email object to stream to attachment

提交回复
热议问题