• .net 图片合成大图


      1 int pheight = ((imageFiles.Count / num) + 1) * (sheight); //计算大图高度
      2 
      3 
      4             List<Rectangle> Rects = new List<Rectangle>();
      5             List<System.Drawing.Image> imgs = new List<System.Drawing.Image>();
      6             System.Drawing.Image img;
      7             System.Drawing.Image sImg;
      8 
      9             try
     10             {
     11 
     12                 if (picPath == "")
     13                 {
     14                     img = new Bitmap(pwidth, pheight);//画颜色背景
     15                 }
     16                 else
     17                 {
     18                     img = Bitmap.FromFile(picPath);//画图片背景
     19                 }
     20 
     21 
     22                 foreach (string item in imageFiles)//加载小图片;
     23                 {
     24                     sImg = Bitmap.FromFile(item);
     25                     imgs.Add(sImg);
     26                 }
     27 
     28                 Pen pen = new Pen(GetColor(borderbg), borderSize);//加边框
     29 
     30 
     31 
     32                 //先画行,再画列
     33                 for (int i = 0; i < pheight; i += sheight)
     34                 {
     35                     for (int j = 2; j < pwidth; j += pwidth / num)
     36                     {
     37                         Rectangle rect = new Rectangle(j, i, swidth, sheight);
     38                         Rects.Add(rect);
     39                     }
     40                 }
     41 
     42 
     43 
     44                 Graphics g = Graphics.FromImage(img);
     45 
     46 
     47                 if (picPath == "")
     48                 {
     49                     g.Clear(GetColor(imagebg));
     50                 }
     51 
     52 
     53 
     54                 string subNum = "";//截取图片后四位做编号
     55                 //根据小图的个数画出
     56                 for (int i = 0; i < imgs.Count; i++)
     57                 {
     58                     subNum = imageFiles[i].Substring(imageFiles[i].LastIndexOf("\\") + 1);
     59                     if (subNum.IndexOf("-") > 0)
     60                     {
     61                         subNum = subNum.Substring(0, subNum.IndexOf("-"));
     62                     }
     63                     else
     64                     {
     65                         subNum = subNum.Substring(0, subNum.IndexOf("."));
     66                     }
     67                     subNum = subNum.Substring(subNum.Length - 4, 4);
     68 
     69                     g.DrawRectangle(pen, Rects[i]);//给每个小图片加上边框
     70                     g.DrawImage(imgs[i], Rects[i]);
     71                     g.DrawString(subNum, new Font("Arial", 10, FontStyle.Bold), new SolidBrush(Color.Red), Rects[i]);
     72                 }
     73 
     74                 string filePath = savePath + "\\" + ProductSKU + "\\" + ProductSKU + ".jpg";//保存路径
     75 
     76                 saveTempPath += "\\" + ProductSKU + ".jpg";//临时工 = =!
     77 
     78 
     79                 MemoryStream ms = new MemoryStream();//放入内存,方便页面显示
     80                 img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
     81 
     82                 if (!Directory.Exists(Path.GetDirectoryName(filePath)))
     83                 {
     84                     Directory.CreateDirectory(Path.GetDirectoryName(filePath));
     85                 }
     86                 
     87                 
     88                 if (File.Exists(filePath))//如果存在就替换
     89                 {
     90                     img.Save(saveTempPath, System.Drawing.Imaging.ImageFormat.Jpeg);//临时工派上上场了
     91                     File.Copy(saveTempPath, filePath, true);
     92                     if (File.Exists(saveTempPath))
     93                     {
     94                         File.Delete(saveTempPath);
     95                     }
     96                 }
     97                 else
     98                 {
     99                     img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);//保存
    100                 }
    101 
    102 
    103                 SplitIMG(filePath, pheight / (sheight), pwidth, sheight, savePath);
    104 
    105                 
    106                 ms.Dispose();
    107                 g.Dispose();
    108                 img.Dispose();
    109 
    110                 foreach (var i in imgs)
    111                 {
    112                     i.Dispose();
    113                 }
    114 
    115                 Response.ClearContent();
    116                 Response.ContentType = "image/Jpeg";
    117                 Response.BinaryWrite(ms.ToArray());
    118             }
    119             catch (Exception ex)
    120             {
    121                 Response.WriteFile(ex.Message);
    122             }
    View Code

    获取文件夹下的图片

     1 public List<string> ImageFile(string path)
     2         {
     3             List<string> PicFile = new List<string>();
     4             FileInfo fi;
     5             if (!Directory.Exists(path))
     6             {
     7                 throw new FileNotFoundException();
     8             }
     9 
    10             foreach (var i in Directory.GetFiles(path))
    11             {
    12                 fi = new FileInfo(i);
    13                 if (fi.Extension.ToLower() == ".jpg" || fi.Extension.ToLower() == ".gif" || fi.Extension.ToLower() == ".bmp")
    14                 {
    15                     PicFile.Add(i);
    16                 }
    17             }
    18 
    19             return PicFile;
    20         }
    View Code

    分成小图

     1 /// <summary>
     2         /// 分成小图
     3         /// </summary>
     4         /// <param name="filePath">图片路径</param>
     5         /// <param name="splitNum">分成几份</param>
     6         /// <param name="w"></param>
     7         /// <param name="h"></param>
     8         /// <param name="savePath"></param>
     9         public void SplitIMG(string filePath, int splitNum, int w, int h, string savePath)
    10         {
    11 
    12             Bitmap bitmap;
    13             Graphics g;
    14 
    15             try
    16             {
    17                 System.Drawing.Image img = Bitmap.FromFile(filePath);
    18 
    19 
    20                 for (int i = 0; i < splitNum; i++)
    21                 {
    22                     using (bitmap = new Bitmap(w, h))
    23                     {
    24                         g = Graphics.FromImage(bitmap);
    25                         g.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(0, h * i, w, h), GraphicsUnit.Pixel);
    26                         bitmap.Save(savePath + "\\" + ProductSKU + "\\" + ProductSKU + "_" + (i + 1) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    27                     }
    28                     g.Dispose();
    29                 }
    30 
    31             }
    32             catch (Exception ex)
    33             {
    34                 Response.Write(ex.Message);
    35                 return;
    36             }
    37             finally
    38             {
    39             }
    40         }
    View Code
  • 相关阅读:
    vim中实现php代码自动完成功能
    tt安装与配置
    GIT 常用命令
    Git命令行基本操作
    传统的分布式应用集成技术(网摘)
    离线安装IE 11
    Intellij IDEA破解激活
    Win7IE11 For x86&x64离线安装包
    Oracle Instant Client(即时客户端) 安装与配置
    转 [OpenCV]:Visual Studio 2012 搭建 OpenCV2.4.3
  • 原文地址:https://www.cnblogs.com/lihui1030/p/3085708.html
Copyright © 2020-2023  润新知