Download server memory image

Asked

Viewed 240 times

8

I have an Asp.Net MVC project where I have an image in memory and would like to download it.

See an excerpt from the code:

Image imagem = default(Image);

using (Bitmap b = new Bitmap(bitmapWidth, bitmapHeight))
{
    using (Graphics g = Graphics.FromImage(b))
    {
        g.Clear(Color.Red);

        var font = new Font("Arial", 50, FontStyle.Bold);
        g.DrawString("Stackoverflow", font, Brushes.White, posicaoX, posicaoY);
    }

    using (MemoryStream stream = new MemoryStream())
    {
        b.Save(stream, ImageFormat.Jpeg);
        imagem = Image.FromStream(stream);
    }
}

using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.DownloadFile(imagem); // Não sei bem como posso fazer o Download.
}
  • Create an action to download.... which version of MVC?

  • What do you mean by "download"? The download should be done on another server. It would bring from elsewhere to here. Would you want to save on the server? Write to another server? That is to say, fzer upload? Want to use somewhere?

  • 1

    This code is running on a server, just wanted to download the file to the client. That’s exactly what is being done in the answer. :)

1 answer

6


In his Controller, create a Action thus:

    public FileResult Imagem()
    {
        /* Carregue o stream antes. */
        return File(stream, "image/jpeg");
    }

Or even, adapting its logic:

public FileResult Imagem()
{
    Image imagem = default(Image);

    using (Bitmap b = new Bitmap(bitmapWidth, bitmapHeight))
    {
        using (Graphics g = Graphics.FromImage(b))
        {
            g.Clear(Color.Red);

            var font = new Font("Arial", 50, FontStyle.Bold);
            g.DrawString("Stackoverflow", font, Brushes.White, posicaoX, posicaoY);
        }

        using (MemoryStream stream = new MemoryStream())
        {
            b.Save(stream, ImageFormat.Jpeg);
            imagem = Image.FromStream(stream);
            return File(stream.ToArray(), "image/jpeg");
        }   
    }
}

EDIT

I don’t know what’s closing the stream, according to your comment, but I would try to carry another stream to return, like this:

using (var ms = new MemoryStream())
{
    imagem.Save(ms,ImageFormat.Jpeg);
    return File(ms.ToArray(), "image/jpeg");
}
  • 1

    haha... thank you so much beast! I’m just waiting to pass the 10 min to be able to accept your reply.

  • There’s only one little thing in return to correct... stream is out of context...

  • 1

    @Jedaiasrodrigues Truth. I edited.

  • Just one question... why I have to return stream instead of imagem?

  • 1

    @Jedaiasrodrigues Because the method File that’s how it is: protected internal FileContentResult File(byte[] fileContents, string contentType);. As you already have one stream Okay, you already have one byte array.

  • I took a look here, that’s right... Thanks again!

  • Just one more problem... I can’t change the return type where this excerpt is (currently it returns Actionresult). I am using MVC 4, and while trying to use return File(stream, "image/jpeg"); within the using an error is generated: Unable to access a closed stream. Description: An untreated exception occurred during the execution of the current web request. Examine stack tracking for more information about the error and where it originated in the code. Exception Details: System.Objectdisposedexception: Unable to access a closed stream.

  • 1

    Strange to close the stream so. I made a suggestion.

  • @Randrade gave it to me a support here in chat.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.