批量修改图片的尺寸(c#)

项目需要,经常需要手动调整图片尺寸,流程太过麻烦 ,效率低下 。所以写了一个小程序,以提高工作效率
 
   
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. namespace ChangeImgSize
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. int num = 0;
  16. if (!System.IO.Directory.Exists("Img"))
  17. {
  18. System.IO.Directory.CreateDirectory("Img");
  19. Console.WriteLine("已创建Img目录,请把需要处理的图片放到该目录下");
  20. Console.ReadKey(true);
  21. return;
  22. }
  23. if (!System.IO.Directory.Exists("NewImg"))
  24. {
  25. System.IO.Directory.CreateDirectory("NewImg");
  26. }
  27. Console.WriteLine("请输入需要缩小的倍数");
  28. double size = Convert.ToDouble(Console.ReadLine());
  29. string[] fileStrs = System.IO.Directory.GetFiles("Img");
  30. foreach (string s in fileStrs)
  31. {
  32. System.IO.FileInfo info = new System.IO.FileInfo(s);
  33. if (info.Extension == ".png" || info.Extension == ".jpg")
  34. {
  35. Image img = GetFile(s);
  36. Bitmap bit = GetNewSizeBitmap(img,size);
  37. SaveImage(bit, info.Name, info.Extension);
  38. }
  39. ++num;
  40. }
  41. Console.WriteLine("共处理:" + num + "张图片,已经保存到NewImg目录中。");
  42. Console.ReadKey(true);
  43. }
  44. static public Image GetFile(string path)
  45. {
  46. FileStream stream = File.OpenRead(path);
  47. int fileLength = 0;
  48. fileLength = (int)stream.Length;
  49. Byte[] image = new Byte[fileLength];
  50. stream.Read(image, 0, fileLength);
  51. System.Drawing.Image result = System.Drawing.Image.FromStream(stream);
  52. stream.Close();
  53. return result;
  54. }
  55. static public Bitmap GetNewSizeBitmap(Image img , double size){
  56. int newWidth = Convert.ToInt32(img.Width / size);
  57. int newHeight = Convert.ToInt32(img.Height / size);
  58. Size s = new Size(newWidth, newHeight);
  59. Bitmap newBit = new Bitmap(img, s);
  60. return newBit;
  61. }
  62. static public void SaveImage(Bitmap bit,string name,string ext)
  63. {
  64. bit.Save(@"NewImg\" + name);
  65. bit.Dispose();
  66. Console.WriteLine("已处理:" + name);
  67. }
  68. }
  69. }





转载于:https://www.cnblogs.com/xiejunzhao/p/d9d4e23ad92300401c3a3cd6456bfd01.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; 问题: C#批量添加图片水印 回答: 在C#中,可以使用System.Drawing命名空间中的类来实现批量添加图片水印的功能。首先,你需要引用System.Drawing命名空间,并使用System.IO命名空间来处理文件操作。然后,你可以使用Bitmap类来加载图片,使用Graphics类来绘制水印,并使用Image类来保存修改后的图片。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.IO; namespace WatermarkExample { class Program { static void Main(string[] args) { string watermarkText = "Watermark"; // 水印文本 string imagePath = "image.jpg"; // 图片路径 string outputDirectory = "output"; // 输出目录 // 创建输出目录 Directory.CreateDirectory(outputDirectory); // 加载图片 using (Bitmap image = new Bitmap(imagePath)) { // 创建Graphics对象 using (Graphics graphics = Graphics.FromImage(image)) { // 设置水印字体和颜色 Font font = new Font("Arial", 12); Brush brush = new SolidBrush(Color.Red); // 计算水印位置 SizeF textSize = graphics.MeasureString(watermarkText, font); PointF position = new PointF(image.Width - textSize.Width - 10, image.Height - textSize.Height - 10); // 绘制水印 graphics.DrawString(watermarkText, font, brush, position); } // 保存修改后的图片 string outputImagePath = Path.Combine(outputDirectory, "watermarked_image.jpg"); image.Save(outputImagePath); } Console.WriteLine("水印添加完成!"); } } } ``` 在上述示例代码中,你可以修改`watermarkText`变量来设置水印文本,`imagePath`变量来设置要添加水印的图片路径,`outputDirectory`变量来设置输出目录。运行代码后,修改后的图片将保存在输出目录中。请确保你已经正确引用了System.Drawing和System.IO命名空间,并且已经将图片文件放置在正确的路径下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值