Aspose.Imaging - 如何在内存中转换图像并发送到浏览器
Aspose.Imaging - How convert image in memory and sent to the browser
我有以下代码来转换图像并将其发送到浏览器。我目前正在将文件保存到服务器,然后发送到浏览器。如何将内存中的图片转换成直接发送给浏览器而不写入服务器?
这是我转换图像、保存文件并发送到浏览器的代码。如何跳过保存到服务器?
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;
API 公开 Image.Save() 方法的重载,该方法 save Image to Stream 而不是写入服务器上的文件。您可以通过保存到 Stream 而不是文件来简单地修改上面的代码。然后将 Stream 转换为字节数组并随后在您的 response.Content 实例化中加载该数组。
using (MemoryStream memory = new MemoryStream())
{
inputImage.Save(memory, imageOptions);
memory.Position = 0;
byte[] arr = memory.ToArray();
}
我还观察到您在 Aspose.Image 支持论坛以及 this thread link 上进行了类似的查询。
我有以下代码来转换图像并将其发送到浏览器。我目前正在将文件保存到服务器,然后发送到浏览器。如何将内存中的图片转换成直接发送给浏览器而不写入服务器?
这是我转换图像、保存文件并发送到浏览器的代码。如何跳过保存到服务器?
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;
API 公开 Image.Save() 方法的重载,该方法 save Image to Stream 而不是写入服务器上的文件。您可以通过保存到 Stream 而不是文件来简单地修改上面的代码。然后将 Stream 转换为字节数组并随后在您的 response.Content 实例化中加载该数组。
using (MemoryStream memory = new MemoryStream())
{
inputImage.Save(memory, imageOptions);
memory.Position = 0;
byte[] arr = memory.ToArray();
}
我还观察到您在 Aspose.Image 支持论坛以及 this thread link 上进行了类似的查询。