DTCMS添加文章时内容中的第一张图片等比例缩放(略)图
时间:6年前 阅读:4620
DTCMS,添加文章时,内容中第一张图片作缩略图
admin/article/article_edit.aspx.cs
导入:
using System.Drawing;
using System.Drawing.Imaging;
private bool DoAdd()方法
//是否将编辑器远程图片保存到本地
if (siteConfig.fileremote == 1)
{
model.content = AutoRemoteImageSave(txtContent.Value);
后面加入:
//设置内容中第一张图片为缩略图
Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
MatchCollection m = reg.Matches(model.content);
foreach (Match math in m)
{
if (math.Index >)
{
string imgUri = math.Groups[1].Value;
if (imgUri != string.Empty)
{
string mini = imgUri.ToLower().Substring(imgUri.LastIndexOf("."));
string path = imgUri.Substring(0, imgUri.Length - mini.Length);
BLL.siteconfig bll_config = new BLL.siteconfig();
Model.siteconfig m_config = bll_config.loadConfig();
int h = m_config.thumbnailheight;
int w = m_config.thumbnailwidth;
Bitmap bmp = new Bitmap(Server.MapPath(imgUri));
Bitmap bmp_new = ZoomImage(bmp, h, w);
switch (mini)
{
case ".jpg": bmp_new.Save(Server.MapPath(path + "_thum" + mini), ImageFormat.Jpeg); break;
case ".gif": bmp_new.Save(Server.MapPath(path + "_thum" + mini), ImageFormat.Gif); break;
}
hidFocusPhoto.Value = path + "_thum" + mini;
break;
}
}
}
添加方法:
#region 等比例缩放图片
private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
{
try
{
System.Drawing.Image sourImage = bitmap;
int width = 0, height = 0;
//按比例缩放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if (sourHeight > destHeight || sourWidth > destWidth)
{
if ((sourWidth * destHeight) > (sourHeight * destWidth))
{
width = destWidth;
height = (destWidth * sourHeight) / sourWidth;
}
else
{
height = destHeight;
width = (sourWidth * destHeight) / sourHeight;
}
}
else
{
width = sourWidth;
height = sourHeight;
}
Bitmap destBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
sourImage.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}
#endregion
本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。
微信扫码关注
更新实时通知
网友评论