0
这是我的模型类取字节[]图片,调整大小和显示在Asp.Net的mvc
public class News: Public
{
[Key]
public int NewsID { get; set; }
[Required]
[StringLength(100, MinimumLength = 2)]
public string Title { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime NewsDate { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[StringLength(1000, MinimumLength = 2)]
public string NewsTXT { get; set; }
public byte[] NewsPic { get; set; }
}
我用它在create
控制器,用于Byte[]
保存图片到数据库
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
news.NewsPic = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
我想让我的控制器中的图像的拇指,并显示在视图中,不希望任何地方保存拇指。
我想要做这样的事,但像从数据库
鉴于获取缩略图控制器
public ActionResult Thumbnail(int width, int height){
var imageFile = Path.Combine(Server.MapPath("~/app_data"), "test.png");
using (var srcImage = Image.FromFile(imageFile))
using (var newImage = new Bitmap(width, height))
using (var graphics = Graphics.FromImage(newImage))
using (var stream = new MemoryStream())
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));
newImage.Save(stream, ImageFormat.Png);
return File(stream.ToArray(), "image/png");
}
}
<img src="@Url.Action("Thumbnail", "SomeController", new { width = 100, height = 50 })" alt="thumb" />
我这个代码的版本是,在拇指控制器中是:
public ActionResult Thumbnail(int width, int height,int id)
{
// TODO: the filename could be passed as argument of course
var photo = db.News.Find(id).NewsPic;
string imageBase64 = Convert.ToBase64String(photo);
var imageSrc = string.Format("data:image/jpg;base64,{0}", imageBase64);
using (var srcImage = Image.FromFile(imageSrc))
using (var newImage = new Bitmap(width, height))
using (var graphics = Graphics.FromImage(newImage))
using (var stream = new MemoryStream())
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));
newImage.Save(stream, ImageFormat.Png);
return File(stream.ToArray(), "image/png");
}
}
我的版本,鉴于:
<img src="@Url.Action("Thumbnail", "News", new { width = 100, height = 50,id=Model.NewsID })" alt="thumb" />
但dosent工作
什么是不工作?你有错误信息吗? –
是的! 'data:image/jpg; base64'不是有效的虚拟路径。 – Ehsan