'Aspose.Imaging - How convert image in memory and sent to the browser
I have the following code to convert an image and sent it to the browser. I'm currently saving the file to the server and then sending to the browser. How can I convert the image in memory and send directly to the browser without writing it to the server?
Here's my code to convert an image, save the file and send to the browser. How can Iskip the saving to the server?
try
{
// 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;
Solution 1:[1]
The API expose the overload of Image.Save() method that can save Image to Stream rather writing to a file on server. You can simply modify your above code by saving to Stream rather to a file. Then converting Stream to Byte Array and loading that array subsequently in your response.Content instantiate.
using (MemoryStream memory = new MemoryStream())
{
inputImage.Save(memory, imageOptions);
memory.Position = 0;
byte[] arr = memory.ToArray();
}
I have also observed that you have made similar inquiry in Aspose.Image support forum as well over this thread link.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wai Ha Lee |