asp.net – MVC3 WebImage助手:resize将透明背景转换为黑色
发布时间:2020-05-24 18:48:42 所属栏目:asp.Net 来源:互联网
导读:我正在尝试使用MVC3的Web Image助手创建缩略图. 原始图像是具有透明背景的.png.当我尝试使用以下内容调整大小时: var image = blob.DownloadByteArray(); new WebImage(image) .Resize(50, 50) .Write(); 生成的缩略图将原始透明背景替换为黑色背景. 上面这
|
我正在尝试使用MVC3的Web Image助手创建缩略图. 原始图像是具有透明背景的.png.当我尝试使用以下内容调整大小时: var image = blob.DownloadByteArray();
new WebImage(image)
.Resize(50,50)
.Write();
生成的缩略图将原始透明背景替换为黑色背景. 解决方法上面这个答案很棒,但我做了一些微调并实现了图像的“保持比例”,这样我们就不会得到拉伸的图像了.using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Helpers;
public static class ResizePng
{
private static IDictionary<string,ImageFormat> _transparencyFormats = new Dictionary<string,ImageFormat>(StringComparer.OrdinalIgnoreCase) { { "png",ImageFormat.Png },{ "gif",ImageFormat.Gif } };
public static WebImage ResizePreserveTransparency(this WebImage image,int width,int height)
{
ImageFormat format = null;
if (!_transparencyFormats.TryGetValue(image.ImageFormat,out format))
{
return image.Resize(width,height);
}
//keep ratio *************************************
double ratio = (double)image.Width / image.Height;
double desiredRatio = (double)width / height;
if (ratio > desiredRatio)
{
height = Convert.ToInt32(width / ratio);
}
if (ratio < desiredRatio)
{
width = Convert.ToInt32(height * ratio);
}
//************************************************
using (Image resizedImage = new Bitmap(width,height))
{
using (Bitmap source = new Bitmap(new MemoryStream(image.GetBytes())))
{
using (Graphics g = Graphics.FromImage(resizedImage))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(source,width,height);
}
}
using (MemoryStream ms = new MemoryStream())
{
resizedImage.Save(ms,format);
return new WebImage(ms.ToArray());
}
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net会员 – 自动生成的密钥不支持散列或加密的密码
- asp.net – 角色提供程序是否按请求缓存?
- asp.net-mvc – 从asp.net POST动作方法更新nhibernate实体
- .net – 传递的主键值的数量必须与实体上定义的主键值的数量
- asp.net-mvc – 我可以在MVC 3中有/多个_Layout页面吗?为c
- asp.net-mvc – 将DropDownListFor绑定到字典
- asp.net-mvc – 如何在html.LabelFor中显示一些文本?
- ASP.NET MVC3 AJAX.BeginForm AjaxOptions OnSuccess OnFai
- asp.net – 有什么功能,将帮助我重构CSS
- asp.net-mvc – 如何使ASP.NET路由转义路由值?
推荐文章
站长推荐
- 在asp.net中使用eval(“”)
- asp.net-mvc – 域模型中的ASP.NET MVC业务逻辑与
- ASP.NET JSON Web令牌“401 Unauthorized”
- asp.net-mvc-3 – 在App_code文件夹中使用razor
- ASP.NET MVC伪本地化
- asp.net-mvc – ASP.NET MVC – RequireJS最佳的
- ASP.NET MVC的提示和ASP.NET WebForms开发人员的
- asp.net – MVP MVC和MVVM之间的区别
- asp.net-mvc – 我应该在Web应用程序中记录哪些信
- asp.net-mvc-3 – 默认模型绑定器没有绑定到字段
热点阅读
