星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageHelper.cs 24KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows;
  9. using System.Windows.Documents;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using VisioForge.Shared.Helpers;
  13. namespace Common.system
  14. {
  15. /// <summary>
  16. /// 图片帮助类
  17. /// 创建人:赵耀
  18. /// 创建时间:2018年11月1日
  19. /// </summary>
  20. public class ImageHelper
  21. {
  22. /// <summary>
  23. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  24. /// </summary>
  25. /// <param name="path">图片位置</param>
  26. /// <returns></returns>
  27. public static Bitmap ReadBitmapFile(string path)
  28. {
  29. try
  30. {
  31. if (File.Exists(path))
  32. {
  33. FileStream fs = File.OpenRead(path); //OpenRead
  34. int filelength = 0;
  35. filelength = (int)fs.Length; //获得文件长度
  36. byte[] image = new byte[filelength]; //建立一个字节数组
  37. fs.Read(image, 0, filelength); //按字节流读取
  38. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  39. fs.Close();
  40. Bitmap bit = new Bitmap(result);
  41. return bit;
  42. }
  43. else
  44. {
  45. return null;
  46. }
  47. }
  48. catch (Exception)
  49. {
  50. return null;
  51. }
  52. }
  53. /// <summary>
  54. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  55. /// </summary>
  56. /// <param name="path">图片位置</param>
  57. /// <returns></returns>
  58. public static Image ReadImageFile(string path)
  59. {
  60. try
  61. {
  62. if (File.Exists(path))
  63. {
  64. FileStream fs = File.OpenRead(path); //OpenRead
  65. int filelength = 0;
  66. filelength = (int)fs.Length; //获得文件长度
  67. byte[] image = new byte[filelength]; //建立一个字节数组
  68. fs.Read(image, 0, filelength); //按字节流读取
  69. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  70. fs.Close();
  71. return result;
  72. }
  73. else
  74. {
  75. return null;
  76. }
  77. }
  78. catch (Exception)
  79. {
  80. return null;
  81. }
  82. }
  83. /// <summary>
  84. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  85. /// </summary>
  86. /// <param name="path">图片位置</param>
  87. /// <returns></returns>
  88. public static BitmapImage ReadBitmapImageFile(string path)
  89. {
  90. BitmapImage bitmap = new BitmapImage();
  91. try
  92. {
  93. using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
  94. {
  95. bitmap = new BitmapImage();
  96. bitmap.DecodePixelHeight = 100;
  97. bitmap.BeginInit();
  98. bitmap.CacheOption = BitmapCacheOption.OnLoad;//设置缓存模式
  99. bitmap.StreamSource = ms;//通过StreamSource加载图片
  100. bitmap.EndInit();
  101. bitmap.Freeze();
  102. }
  103. return bitmap;
  104. }
  105. catch (Exception)
  106. {
  107. return null;
  108. }
  109. }
  110. #region 获取RGB
  111. private static Bitmap _Bitmap = null;
  112. private static StringBuilder sb = new StringBuilder();
  113. public static string GetRGB(int x, int y)
  114. {
  115. sb = new StringBuilder();
  116. try
  117. {
  118. System.Drawing.Color color = _Bitmap.GetPixel(x, y);
  119. sb.Append("RGB:(");
  120. sb.Append(color.R.ToString());
  121. sb.Append(",");
  122. sb.Append(color.G.ToString());
  123. sb.Append(",");
  124. sb.Append(color.B.ToString());
  125. sb.Append(")");
  126. }
  127. catch (Exception ex)
  128. {
  129. LogHelper.WriteErrLog("ImageHelper(GetRGB)" + ex.Message, ex);
  130. }
  131. return sb.ToString();
  132. }
  133. #endregion 获取RGB
  134. #region 截图统一方法
  135. /// <summary>
  136. /// 截图通用方法 创建人:赵耀 创建时间:2020年8月11日
  137. /// </summary>
  138. /// <param name="ScreenSize">截图的区域,设置new Rectangle(0, 0, 0, 0)为截全屏</param>
  139. /// <param name="ImageSavePath">图片存储路径,为空或null则保存到临时文件夹</param>
  140. /// <param name="IsRetImg">是否返回图片</param>
  141. /// <param name="bitmapimg">图片</param>
  142. /// <param name="level">压缩等级,0到100,0 最差质量,100 最佳</param>
  143. /// <returns></returns>
  144. public static bool GetScreenshot(Rectangle ScreenSize, string ImageSavePath, bool IsRetImg, out BitmapImage bitmapimg, long level = -1)
  145. {
  146. bitmapimg = null;
  147. try
  148. {
  149. System.Drawing.Size bounds = PrimaryScreen.DESKTOP;
  150. if (string.IsNullOrEmpty(ImageSavePath))
  151. {
  152. ImageSavePath = GetTempImagePath();//如果为空则指定临时存储路径
  153. }
  154. double scaleWidth = (bounds.Width * 1.0) / SystemParameters.PrimaryScreenWidth;
  155. double scaleHeight = (bounds.Height * 1.0) / SystemParameters.PrimaryScreenHeight;
  156. int width = bounds.Width;
  157. int height = bounds.Height;
  158. if (ScreenSize.Size != new System.Drawing.Size(0, 0))
  159. {
  160. width = (int)(ScreenSize.Size.Width * scaleWidth);
  161. height = (int)(ScreenSize.Size.Height * scaleHeight);
  162. }
  163. int l = (int)(ScreenSize.X * scaleWidth);
  164. int t = (int)(ScreenSize.Y * scaleHeight);
  165. if (_Bitmap != null)
  166. {
  167. _Bitmap.Dispose();
  168. }
  169. _Bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  170. using (Graphics g = Graphics.FromImage(_Bitmap))
  171. {
  172. g.CopyFromScreen(l, t, 0, 0, _Bitmap.Size, CopyPixelOperation.SourceCopy);
  173. Compress(_Bitmap, ImageSavePath, level);
  174. }
  175. if (IsRetImg)
  176. {
  177. Uri uri = new Uri(ImageSavePath, UriKind.Absolute);
  178. bitmapimg = new BitmapImage(uri);
  179. }
  180. GC.Collect();
  181. return true;
  182. }
  183. catch (Exception ex)
  184. {
  185. LogHelper.WriteErrLog("【截图】(GetBitmapSource)截图失败," + ex.Message, ex);
  186. return false;
  187. }
  188. }
  189. /// <summary>
  190. /// 获取临时图片保存位置
  191. /// </summary>
  192. /// <returns></returns>
  193. public static string GetTempImagePath()
  194. {
  195. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  196. string TempPath = FileToolsCommon.GetFileAbsolutePath("/temp/Screenshot/");
  197. FileToolsCommon.CreateDirectory(TempPath);
  198. TempPath += Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".jpg";
  199. return TempPath;
  200. }
  201. #endregion 截图统一方法
  202. #region 图片压缩
  203. /// <summary>
  204. /// 图片压缩(降低质量以减小文件的大小) 创建人:赵耀 创建时间:2020年8月11日
  205. /// </summary>
  206. /// <param name="srcBitMap">传入的Bitmap对象</param>
  207. /// <param name="destFile">压缩后的图片保存路径</param>
  208. /// <param name="level">压缩等级,-1为config配置的值,0到100,0 最差质量,100 最佳</param>
  209. public static void Compress(Bitmap srcBitMap, string destFile, long level)
  210. {
  211. if (level <= -1)
  212. {
  213. int ImageCompressionLevel = int.Parse(FileToolsCommon.GetConfigValue("ImageCompressionLevel"));
  214. level = ImageCompressionLevel;
  215. }
  216. Stream s = new FileStream(destFile, FileMode.Create);
  217. Compress(srcBitMap, s, level);
  218. s.Close();
  219. }
  220. /// <summary>
  221. /// 图片压缩(降低质量以减小文件的大小) 创建人:赵耀 创建时间:2020年8月11日
  222. /// </summary>
  223. /// <param name="srcBitmap">传入的Bitmap对象</param>
  224. /// <param name="destStream">压缩后的Stream对象</param>
  225. /// <param name="level">压缩等级,0到100,0 最差质量,100 最佳</param>
  226. public static void Compress(Bitmap srcBitmap, Stream destStream, long level)
  227. {
  228. ImageCodecInfo myImageCodecInfo;
  229. System.Drawing.Imaging.Encoder myEncoder;
  230. EncoderParameter myEncoderParameter;
  231. EncoderParameters myEncoderParameters;
  232. //获取表示jpeg编解码器的imagecodecinfo对象
  233. myImageCodecInfo = GetEncoderInfo("image/jpeg");
  234. myEncoder = System.Drawing.Imaging.Encoder.Quality;
  235. myEncoderParameters = new EncoderParameters(1);
  236. myEncoderParameter = new EncoderParameter(myEncoder, level);
  237. myEncoderParameters.Param[0] = myEncoderParameter;
  238. srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
  239. }
  240. /// <summary>
  241. /// 获取解码器 创建人:赵耀 创建时间2020年8月11日
  242. /// </summary>
  243. /// <param name="mimeType"></param>
  244. /// <returns></returns>
  245. private static ImageCodecInfo GetEncoderInfo(string mimeType)
  246. {
  247. int j;
  248. ImageCodecInfo[] encoders;
  249. encoders = ImageCodecInfo.GetImageEncoders();
  250. for (j = 0; j < encoders.Length; ++j)
  251. {
  252. if (encoders[j].MimeType == mimeType)
  253. {
  254. return encoders[j];
  255. }
  256. }
  257. return null;
  258. }
  259. /// <summary>
  260. /// 压缩图片 -测试方法 待完善 暂无用
  261. /// </summary>
  262. /// <param name="_bitmap">原图片</param>
  263. /// <param name="dFile">压缩后保存图片地址</param>
  264. /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
  265. /// <param name="size">压缩后图片的最大大小</param>
  266. /// <param name="sfsc">是否是第一次调用</param>
  267. /// <returns></returns>
  268. public static bool CompressImage(Bitmap _bitmap, string dFile, int flag = 90, int size = 300)
  269. {
  270. ////如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
  271. //FileInfo firstFileInfo = new FileInfo(sFile);
  272. //if (sfsc == true && firstFileInfo.Length < size * 1024)
  273. //{
  274. // firstFileInfo.CopyTo(dFile);
  275. // return true;
  276. //}
  277. //Image iSource = Image.FromFile(sFile);
  278. Image iSource = _bitmap;
  279. ImageFormat tFormat = iSource.RawFormat;
  280. int dHeight = iSource.Height / 2;
  281. int dWidth = iSource.Width / 2;
  282. int sW, sH;
  283. //按比例缩放
  284. System.Drawing.Size tem_size = new System.Drawing.Size(iSource.Width, iSource.Height);
  285. if (tem_size.Width > dHeight || tem_size.Width > dWidth)
  286. {
  287. if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
  288. {
  289. sW = dWidth;
  290. sH = (dWidth * tem_size.Height) / tem_size.Width;
  291. }
  292. else
  293. {
  294. sH = dHeight;
  295. sW = (tem_size.Width * dHeight) / tem_size.Height;
  296. }
  297. }
  298. else
  299. {
  300. sW = tem_size.Width;
  301. sH = tem_size.Height;
  302. }
  303. Bitmap ob = new Bitmap(dWidth, dHeight);
  304. Graphics g = Graphics.FromImage(ob);
  305. g.Clear(System.Drawing.Color.WhiteSmoke);
  306. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  307. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  308. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  309. g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
  310. g.Dispose();
  311. //以下代码为保存图片时,设置压缩质量
  312. EncoderParameters ep = new EncoderParameters();
  313. long[] qy = new long[1];
  314. qy[0] = flag;//设置压缩的比例1-100
  315. EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
  316. ep.Param[0] = eParam;
  317. try
  318. {
  319. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
  320. ImageCodecInfo jpegICIinfo = null;
  321. for (int x = 0; x < arrayICI.Length; x++)
  322. {
  323. if (arrayICI[x].FormatDescription.Equals("JPEG"))
  324. {
  325. jpegICIinfo = arrayICI[x];
  326. break;
  327. }
  328. }
  329. if (jpegICIinfo != null)
  330. {
  331. if (flag > 0)
  332. {
  333. ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
  334. FileInfo fi = new FileInfo(dFile);
  335. if (fi.Length > 1024 * size)
  336. {
  337. flag -= 10;
  338. CompressImage(_bitmap, dFile, flag, size);
  339. }
  340. }
  341. //ob.Save(dFile, jpegICIinfo, ep);
  342. //FileInfo fi = new FileInfo(dFile);
  343. //bool IsAgain = false;
  344. //if (fi.Length > 1024 * size)
  345. //{
  346. // fi.CopyTo(dFile + fi.Length);
  347. // fi.Delete();
  348. // Bitmap newbitmap = ReadImageFile(dFile + fi.Length);
  349. // CompressImage(newbitmap, dFile, flag, size);
  350. // FileToolsCommon.DeleteFile(dFile + fi.Length);
  351. //}
  352. }
  353. else
  354. {
  355. ob.Save(dFile, tFormat);
  356. }
  357. return true;
  358. }
  359. catch
  360. {
  361. return false;
  362. }
  363. finally
  364. {
  365. iSource.Dispose();
  366. ob.Dispose();
  367. }
  368. }
  369. #endregion 图片压缩
  370. #region 截屏指定UI控件
  371. /// <summary>
  372. /// 保存图片
  373. /// </summary>
  374. /// <param name="ui">需要截图的UI控件</param>
  375. /// <param name="filePathName">图片保存地址 命名 1.png</param>
  376. /// <param name="width">窗口宽</param>
  377. /// <param name="height">窗口高</param>
  378. /// <param name="ImgWidth">图片高</param>
  379. /// <param name="ImgHeight">图片高</param>
  380. public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
  381. {
  382. try
  383. {
  384. //在位图中呈现UI元素
  385. RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
  386. bmp.Render(ui);
  387. BitmapEncoder encoder = new PngBitmapEncoder();
  388. encoder.Frames.Add(BitmapFrame.Create(bmp));
  389. if (ImgWidth > 0)
  390. {
  391. Bitmap Img = new Bitmap(ImgWidth, ImgHeight);
  392. try
  393. {
  394. MemoryStream memoryStream = new MemoryStream();
  395. encoder.Save(memoryStream);
  396. //System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
  397. Bitmap bit = new Bitmap(memoryStream);
  398. if ((int)ImgWidth-2 < (int)bit.Width)
  399. {
  400. try
  401. {
  402. Graphics g = Graphics.FromImage(Img);
  403. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  404. g.DrawImage(bit, new Rectangle(0, 0, ImgWidth, ImgHeight), new Rectangle(0, 0, (int)bit.Width, (int)bit.Height), GraphicsUnit.Pixel);
  405. g.Dispose();
  406. }
  407. catch
  408. {
  409. Img = bit;
  410. }
  411. }
  412. else
  413. {
  414. Img = bit;
  415. }
  416. new Thread(new ThreadStart(new Action(() =>
  417. {
  418. Img.Save(filePathName);
  419. //Bitmap bitmap = CutImageWhitePart(Img);
  420. //FileToolsCommon.DeleteFile(filePathName);
  421. //bitmap.Save(filePathName);
  422. //bitmap.Dispose();
  423. memoryStream.Dispose();
  424. Img.Dispose();
  425. bit.Dispose();
  426. }))).Start();
  427. }
  428. catch (Exception)
  429. {
  430. }
  431. }
  432. else
  433. {
  434. //SaveImageModel sim = new SaveImageModel();
  435. //sim.encoder = new PngBitmapEncoder();
  436. //sim.encoder = new PngBitmapEncoder();
  437. //BitmapFrame test= encoder.Frames[0];
  438. //sim.encoder.Frames.Add(BitmapFrame.Create(test));
  439. ////sim.encoder.Frames[0].CopyTo(encoder.Frames[0]);
  440. ////sim.encoder.CopyTo(encoder);
  441. ////encoder.CopyTo(sim.encoder);
  442. //sim.FilePath = filePathName;
  443. ////sim.bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
  444. ////bmp.CopyTo(sim.bmp);
  445. //Thread t1 = new Thread(SaveImage);
  446. //t1.Start(sim);
  447. System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
  448. encoder.Save(fs);
  449. fs.Close();
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. LogHelper.WriteErrLog("【UI生成图片】(SaveUIToImage)" + ex.Message, ex);
  455. //Console.WriteLine(ex.Message);
  456. }
  457. }
  458. private static void SaveImage(object obj)
  459. {
  460. SaveImageModel sim = (SaveImageModel)obj;
  461. //RenderTargetBitmap bmp = sim.bmp;
  462. //BitmapEncoder encoder = new PngBitmapEncoder();
  463. //encoder.Frames.Add(BitmapFrame.Create(bmp));
  464. BitmapEncoder encoder = sim.encoder;
  465. string filePathName = sim.FilePath;
  466. System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
  467. encoder.Save(fs);
  468. fs.Close();
  469. }
  470. #endregion
  471. #region 去掉白边
  472. /// <summary>
  473. /// 裁剪图片(去掉白边)
  474. /// </summary>
  475. /// <param name="FilePath"></param>
  476. public static Bitmap CutImageWhitePart(Bitmap bmp)
  477. {
  478. //Bitmap bmp = new Bitmap(FilePath);
  479. //上左右下
  480. int top = 0, left = 0, right = bmp.Width, bottom = bmp.Height;
  481. //寻找最上面的标线,从左(0)到右,从上(0)到下
  482. for (int i = 0; i < bmp.Height; i++)//行
  483. {
  484. bool find = false;
  485. for (int j = 0; j < bmp.Width; j++)//列
  486. {
  487. System.Drawing.Color c = bmp.GetPixel(j, i);
  488. if (!IsWhite(c))
  489. {
  490. top = i;
  491. find = true;
  492. break;
  493. }
  494. }
  495. if (find)
  496. {
  497. break;
  498. }
  499. }
  500. //寻找最左边的标线,从上(top位)到下,从左到右
  501. for (int i = 0; i < bmp.Width; i++)//列
  502. {
  503. bool find = false;
  504. for (int j = top; j < bmp.Height; j++)//行
  505. {
  506. System.Drawing.Color c = bmp.GetPixel(i, j);
  507. if (!IsWhite(c))
  508. {
  509. left = i;
  510. find = true;
  511. break;
  512. }
  513. }
  514. if (find)
  515. {
  516. break;
  517. }
  518. }
  519. //寻找最下边标线,从下到上,从左到右
  520. for (int i = bmp.Height - 1; i >= 0; i--)//行
  521. {
  522. bool find = false;
  523. for (int j = left; j < bmp.Width; j++)//列
  524. {
  525. System.Drawing.Color c = bmp.GetPixel(j, i);
  526. if (!IsWhite(c))
  527. {
  528. bottom = i;
  529. find = true;
  530. break;
  531. }
  532. }
  533. if (find)
  534. {
  535. break;
  536. }
  537. }
  538. //寻找最右边的标线,从上到下,从右往左
  539. for (int i = bmp.Width - 1; i >= 0; i--)//列
  540. {
  541. bool find = false;
  542. for (int j = 0; j <= bottom; j++)//行
  543. {
  544. System.Drawing.Color c = bmp.GetPixel(i, j);
  545. if (!IsWhite(c))
  546. {
  547. right = i;
  548. find = true;
  549. break;
  550. }
  551. }
  552. if (find)
  553. {
  554. break;
  555. }
  556. }
  557. if (right - left <= 0)//zxyceshi
  558. {
  559. //克隆位图对象的一部分。
  560. System.Drawing.Rectangle cloneRect = new System.Drawing.Rectangle(left, top, 1, bottom - top);
  561. Bitmap cloneBitmap = bmp.Clone(cloneRect, bmp.PixelFormat);
  562. bmp.Dispose();
  563. //cloneBitmap.Save(@"d:\123.png", ImageFormat.Png);
  564. return cloneBitmap;
  565. }
  566. else
  567. {
  568. //克隆位图对象的一部分。
  569. System.Drawing.Rectangle cloneRect = new System.Drawing.Rectangle(left, top, right - left, bottom - top);
  570. Bitmap cloneBitmap = bmp.Clone(cloneRect, bmp.PixelFormat);
  571. bmp.Dispose();
  572. //cloneBitmap.Save(@"d:\123.png", ImageFormat.Png);
  573. return cloneBitmap;
  574. }
  575. }
  576. /// <summary>
  577. /// 判断是否白色和纯透明色(10点的容差)
  578. /// </summary>
  579. public static bool IsWhite(System.Drawing.Color c)
  580. {
  581. //纯透明也是白色,RGB都为255为纯白
  582. if (c.A < 10 || (c.R > 245 && c.G > 245 && c.B > 245))
  583. {
  584. return true;
  585. }
  586. return false;
  587. }
  588. #endregion
  589. }
  590. public class SaveImageModel
  591. {
  592. public string FilePath { get; set; }
  593. public BitmapEncoder encoder { get; set; }
  594. //public RenderTargetBitmap bmp { get; set; }
  595. }
  596. }