C#里如何修改图片(以路径形式读取的图片)的长宽(大小)?

longliming1985 2008-10-13 09:56:19
因为我要录入数据库,日后要进行统一操作!
...全文
574 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjq 2008-10-13
  • 打赏
  • 举报
回复
这是批量改图片尺寸的:

调用方法: ResizeImages(包含原始图片的路经,存放修改大小后图片的路经,图片的最大尺寸(大于该尺寸的就压缩),输出文件的质量,是否处理子文件夹里的图片)


 public static void SaveFileAsJPG(string orgionalExt, System.IO.Stream file, System.Drawing.Size size, long quality, ref FileStream targetFile)
{
orgionalExt = orgionalExt.Replace(".", "").ToLower();
System.Drawing.Graphics g;
System.Drawing.Image img;
if (orgionalExt == "bmp")
{
img = System.Drawing.Bitmap.FromStream(file);
if (size.Width == 0 || size.Height == 0)
{
size.Width = img.Width;
size.Height = img.Height;
}
}
else
{
img = System.Drawing.Image.FromStream(file);
if (size.Width == 0 || size.Height == 0)
{
size.Width = img.Width;
size.Height = img.Height;
}
}
System.Drawing.Image img1;
if (img.Width > size.Width || img.Height > size.Height)
{
if (img.Width > img.Height)
{
float h = ((float)size.Width / img.Width * img.Height);
img1 = new System.Drawing.Bitmap(size.Width, (int)h);
}
else
{
float w = ((float)size.Height / img.Height * img.Width);
img1 = new System.Drawing.Bitmap((int)w, size.Height);
}


g = System.Drawing.Graphics.FromImage(img1);
g.DrawImage(img, 0, 0, (float)img1.Width, (float)img1.Height);
}
else
{
img1 = img;
g = System.Drawing.Graphics.FromImage(img1);
}
g.Save();
System.IO.Stream srm = targetFile;
System.Drawing.Imaging.ImageCodecInfo ici = GetCodecInfo("image/jpeg");
EncoderParameters parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
img1.Save(srm, ici, parameters);
srm.Position = 0;
}
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}

private void ResizeImages(string path, string target, Size size, long quality, bool subfolder)
{
if (!Directory.Exists(target))
Directory.CreateDirectory(target);
List<string> lst = new List<string>();
lst.AddRange(Directory.GetFiles(path, "*.jpg", subfolder ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));
lst.AddRange(Directory.GetFiles(path, "*.bmp", subfolder ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));
for (int i = 0; i < lst.Count; i++)
{
label7.Text = string.Format("{0}/{1}", i, lst.Count);
Application.DoEvents();
string ext = Path.GetExtension(lst[i]);
string tarfile = target + Path.GetFileNameWithoutExtension(lst[i]) + "_" + i.ToString() + ".jpg";
if (File.Exists(tarfile))
{
if (checkBox2.Checked)
{
File.SetAttributes(tarfile,FileAttributes.Normal );
File.Delete(tarfile);
}
else
continue;
}
FileStream fs = File.OpenWrite(tarfile );
SaveFileAsJPG(ext, File.OpenRead(lst[i]), size, quality, ref fs);
fs.Flush();
fs.Close();
}
}
Ki1381 2008-10-13
  • 打赏
  • 举报
回复
DrawImage
TLJewel 2008-10-13
  • 打赏
  • 举报
回复
帮顶~!
不是很会
花落_ 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoujk 的回复:]
读入图片到IMAGE imgA,
新建一个IMAGE imgB,指定它的长宽。
将imgA的数据写到 imgB,如果其中源的长或宽比目标的要小,就需要另加裁减代码。
然后使用 imgB.Save() 函数将 imgA的源文件覆盖即可。
[/Quote]
支持,
这样最简单//
zhoujk 2008-10-13
  • 打赏
  • 举报
回复
读入图片到IMAGE imgA,
新建一个IMAGE imgB,指定它的长宽。
将imgA的数据写到 imgB,如果其中源的长或宽比目标的要小,就需要另加裁减代码。
然后使用 imgB.Save() 函数将 imgA的源文件覆盖即可。

110,535

社区成员

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

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

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