Omar Little
Omar Little

Reputation: 71

Attach multiple image formats, not just JPG to email

This is what i have so far...

 MemoryStream imgStream = new MemoryStream();
        System.Drawing.Image img = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
        string filename = fuImage.PostedFile.FileName;

        img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        imgStream.Seek(0L, SeekOrigin.Begin);
        EmailMsg.Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg));

So it attaches a JPEG image fine. But i want to change it so that it allows PNG's and GIFs.

Upvotes: 4

Views: 5559

Answers (2)

mtoregan
mtoregan

Reputation: 103

You've probably have found this answer by now, so this answer is for anyone else who runs into this issue.

    Image image = ConvertToImage(response);
    Stream imageStream = ToStream(image, ImageFormat.Png);
    LinkedResource logo = new LinkedResource(imageStream, "image/png");

Above is a sample worked for me, ConvertToImage returns System.Drawing.Image and ToStream returns System.IO.Stream.

I hope this helps!

Upvotes: 4

Ethan Korolyov
Ethan Korolyov

Reputation: 323

try this:

 string ext = System.IO.Path.GetExtension(this.imageUpload.PostedFile.FileName);

 string low = ext.ToLower();

 MemoryStream imgStream = new MemoryStream();

                System.Drawing.Image theImage = System.Drawing.Image.FromStream(imageUpload.PostedFile.InputStream);

                string filename = imageUpload.PostedFile.FileName;

              if (low == ".jpg")
                {
                    theImage.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                }
                else if (low == ".gif")
                {
                    theImage.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);
                }
                else if (low == ".tif")
                {
                    theImage.Save(imgStream, System.Drawing.Imaging.ImageFormat.Tiff);
                }

            imgStream.Seek(0L, SeekOrigin.Begin);

                if (low == ".jpg")
                {

                    mMailMessage.Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg));
                }

                else if (low == ".gif")
                {

                    mMailMessage.Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Gif));
                }

 else if (low == ".tif")
                {

                    mMailMessage.Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Tiff));
                }

doing .png seems a little trickier, but at least that's on the right track.

Upvotes: 2

Related Questions