图片处理

 Path.Combine(filePath, imgUrl);//将两个字符串组合成一个路径

1.下载网络图片

WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData("http://baidu.png");//网络地址
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

2.获取Image

			//Linux 的地址 /app/imgs/1.png
			var path="C:\1.png";//绝对路径
			Image.FromFile(path);

3.获取文件流
(1)

			var fileName="C://1.png";//绝对路径
            Bitmap bmp = new Bitmap(fileName);
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, bmp.RawFormat);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);//可异步
            ms.Close();

(2)

				var fileName="C://1.png";//绝对路径
 				FileStream fileStream = new FileStream(fileName, FileMode.Open);

                // 读取文件的 byte[]

                byte[] bytes = new byte[fileStream.Length];

                fileStream.Read(bytes, 0, bytes.Length);

                fileStream.Close();

                // 把 byte[] 转换成 Stream

                Stream stream = new MemoryStream(bytes);
                System.Drawing.Image imag = System.Drawing.Image.FromStream(stream);

3.Html 转 图片

	
 public static bool GetImage(string url, string imgPath)
        {
           //Linux
            var p = new Process
            {
                StartInfo =
                {
                    FileName = "/app/exec/bin/phantomjs",//需要下载 phantomjs 
                    Arguments = $"/app/exec/examples/rasterize.js {url} {imgPath}", //需要下载 rasterize.js
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }
            };
            p.Start();
            p.Close();
            return true;



			//windows
			//程序文件Dir
            var binDir = ConstValues.GetRootPath("exec");//根目录查找exec
            var args = $"--ignore-ssl-errors=yes \"{binDir}\\rasterize.js\" \"{url}\" \"{imgPath}\"";

            var p = new Process
            {
                StartInfo =
                {
                    FileName = binDir + "\\phantomjs.exe",
                    WorkingDirectory = binDir,
                    Arguments = args,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden
                },
            };
            p.Start();
            p.WaitForExit(60 * 1000);
            return true;
        }
       
  1. 切片
    ///
    /// 切片
    ///
    /// 图片路径
    /// 切片宽
    /// 切片高
    public static List SliceUp(Image orginalImage, int silceUpWidth, int silceUpHeight, long productId, string filePath)
    {
    List lstImgPath = new List();

         //计算x方向上的切片份数,如果原图宽度100 ,切片宽度30,则切片在x方向上的份数为4
         int widthCount = (int)Math.Ceiling((orginalImage.Width * 1.0) / (silceUpWidth * 1.0));
         //计算y方向上的切片份数,如果原图宽度100 ,切片宽度30,则切片在x方向上的份数为4
         int heightCount = (int)Math.Ceiling((orginalImage.Height * 1.0) / (silceUpHeight * 1.0));
         for (int i = 0; i < heightCount; i++)
         {
             //设置最终切割的高度
             int recHeight = 0;
             //判断图片高度是否满足本次切片的高度
             if (orginalImage.Height - silceUpHeight * i > silceUpHeight)
             {
                 recHeight = silceUpHeight;
             }
             else
             {
                 recHeight = orginalImage.Height - silceUpHeight * i;
             }
             for (int j = 0; j < widthCount; j++)
             {
                 //设置最终切割的宽度
                 int recWidth = silceUpWidth;
                 //保留exif元数据,初始化ImageFactory
                 using (ImageFactory imageFactory = new ImageFactory(true))
                 {
                     //创建截取图片的大小
                     Rectangle rectangle = new Rectangle
                         (j * silceUpWidth
                         , i * silceUpHeight
                         , recWidth, recHeight
                         );
                     var imgUrl = $"{SequNo.NewId52}//{productId}//{i}.png";
                     var path = Path.Combine(filePath, imgUrl);
                     //切割图片
                     imageFactory.Load(orginalImage)
                         .Crop(rectangle)
                        .Save(path);
                     lstImgPath.Add(path);
                 }
             }
         }
         return lstImgPath;
     }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值