Free Support Forum -

Convert image and output to browser

How can I convert a file and then stream it to the browser to be opened or downloaded. I have the following code to convert a file, BUT I have to save it to the server before sending to the browser. How can I skip the saving/converting the file to the server? and just convert the image/file in memory and send to the browser?


** {**

** // Convert to JPG**
** using (Aspose.Imaging.Image inputImage = Aspose.Imaging.Image.Load(filePath))**
** {**
** JpegOptions imageOptions = new JpegOptions();**

** inputImage.Save(imageFile + Path.GetFileName(filePath).Replace(".bin", “.JPG”), imageOptions);**
** }**
** filePath = imageFile + Path.GetFileName(filePath).Replace(".bin", “.JPG”);**
** }**
** catch (Exception ex)**
** {**
** return new HttpResponseMessage(HttpStatusCode.InternalServerError);**

** }**

** //Read the File into a Byte Array.**
** byte[] bytes = File.ReadAllBytes(filePath);**

** //Set the Response Content.**
** response.Content = new ByteArrayContent(bytes);**

** //Set the Response Content Length.**
** response.Content.Headers.ContentLength = bytes.LongLength;**

** //Set the Content Disposition Header Value and FileName.**
** response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(“attachment”);**
** response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(filePath);**

** //Set the File Content Type.**
** response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(Path.GetFileName(filePath)));**
** return response;**


Please check this API reference guide link for different overloads of Save method in Image class. As far as API is concerned, it can save both on disk and in Stream. Displaying the saved image in browser is out of scope of API as there is no ResponseStream output of Image class. However, you can save image to MemoryStream and then get byte array from stream that you have already used in your further implementation.

            using (MemoryStream memory = new MemoryStream())
                inputImage.Save(memory, imageOptions);
                memory.Position = 0;
                byte[] arr = memory.ToArray();