我正在使用mvc2,我想在控制器中使用动作,例如ShowSmallImage)
,当我输入www.url.com/ShowSmallImage时,在浏览器中输出是一个图像。
我试过这样的事情:
public Bitmap CreateThumbnail()
{
Image img1 = Image.FromFile(@"C:...\Uploads\Photos\178.jpg");
int newWidth = 100;
int newHeight = 100;
double ratio = 0;
if (img1.Width > img1.Height)
{
ratio = img1.Width / (double)img1.Height;
newHeight = (int)(newHeight / ratio);
}
else
{
ratio = img1.Height / (double)img1.Width;
newWidth = (int)(newWidth / ratio);
}
//a holder for the result
Bitmap result = new Bitmap(newWidth, newHeight);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(img1, 0, 0, result.Width, result.Height);
}
return result;
}
因此我在浏览器中只获得System.Drawing.Bitmap。我想我需要设置页面的响应/内容类型,但不知道如何做...
谢谢,
ILE
答案 0 :(得分:3)
创建一个fileresult并将流返回到位图&设置内容类型:
private FileResult RenderImage()
{
MemoryStream stream = new MemoryStream();
var bitmap = CreateThumbnail();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] bytes = stream.ToArray();
return File(bytes, "image/png");
}
答案 1 :(得分:1)
在控制器中,例如ResourceController
,您可以Action
返回FileResult
。像这样
public FileResult Thumbnail()
{
var bitmap = // Your method call which returns a Bitmap
var ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
return new FileStreamResult(ms, "image/png");
}
然后你可以致电http://www.mysite.com/Resource/Thumbnail
。