怎样把大小不一样的 图片生成大小一样但不变形的图片类似于windows下缩略图的效果在线等

youdiving 2012-05-19 09:53:32
C#
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
youdiving 2012-05-19
  • 打赏
  • 举报
回复
oImg = oImg.GetThumbnailImage(100, 100, null, IntPtr.Zero);
这种方法生成的图片变形啊,这里用到string strGuid = System.Guid.NewGuid().ToString().ToUpper();strGuid是为了产生不同的文件名才用的吗
youdiving 2012-05-19
  • 打赏
  • 举报
回复
我试试,谢谢
孟子E章 2012-05-19
  • 打赏
  • 举报
回复
System.Drawing.Image oImg = System.Drawing.Image.FromFile(strFileName)
oImg = oImg.GetThumbnailImage(100, 100, null, IntPtr.Zero);
string strGuid = System.Guid.NewGuid().ToString().ToUpper();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
MemoryStream MemStream = new MemoryStream();
oImg.Save(strGuid + strFileExt,System.Drawing.Imaging.ImageFormat.Jpeg);
oImg.Dispose();
孟子E章 2012-05-19
  • 打赏
  • 举报
回复
等比例的缩略图的方法

public void MakeThumbnail(string imgPath_old, int width, int height)
{
//
System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imgPath_old)));

int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
//以jpg格式保存缩略图
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}


调用方法

MakeThumbnail("d:\\aaa.jpg", 100, 100);
}

注意这里覆盖了原来的图片,不想覆盖的话,就自己换一个文件名就可以了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧