ImageHelper.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. 
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode
  9. {
  10. /// <summary>
  11. /// 图片操作类
  12. /// </summary>
  13. public static class ImageHelper
  14. {
  15. #region 平铺
  16. // 水平 左 中 右
  17. // 垂直 上 中 下
  18. /// <summary>
  19. /// 平铺
  20. /// </summary>
  21. /// <param name="image"></param>
  22. /// <param name="size"></param>
  23. /// <param name="alignment"></param>
  24. /// <returns></returns>
  25. public static Image Tile(Image image, Size size, ContentAlignment alignment = ContentAlignment.TopLeft)
  26. {
  27. Image targetImage = new Bitmap(size.Width, size.Height);
  28. Tile(image, targetImage, null, alignment);
  29. return targetImage;
  30. }
  31. /// <summary>
  32. /// 平铺
  33. /// </summary>
  34. /// <param name="image"></param>
  35. /// <param name="targetImage"></param>
  36. /// <param name="rectangle"></param>
  37. /// <param name="alignment"></param>
  38. public static void Tile(Image image, Image targetImage, Rectangle? rectangle = null,
  39. ContentAlignment alignment = ContentAlignment.TopLeft)
  40. {
  41. if (rectangle == null)
  42. {
  43. rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
  44. }
  45. else
  46. {
  47. rectangle = Rectangle.Intersect(rectangle.Value,
  48. new Rectangle(0, 0, targetImage.Width, targetImage.Height));
  49. if (rectangle.Value.IsEmpty)
  50. {
  51. return;
  52. }
  53. }
  54. using (Graphics graphics = Graphics.FromImage(targetImage))
  55. {
  56. Tile(image, graphics, rectangle.Value, alignment);
  57. }
  58. }
  59. /// <summary>
  60. /// 平铺
  61. /// </summary>
  62. /// <param name="image"></param>
  63. /// <param name="graphics"></param>
  64. /// <param name="rectangle"></param>
  65. /// <param name="alignment"></param>
  66. public static void Tile(Image image, Graphics graphics, Rectangle rectangle,
  67. ContentAlignment alignment = ContentAlignment.TopLeft)
  68. {
  69. using (TextureBrush brush = new TextureBrush(image))
  70. {
  71. SetGraphicsHighQuality(graphics);
  72. brush.WrapMode = WrapMode.Tile;
  73. //if (alignment == ContentAlignment.TopLeft)
  74. //{
  75. // graphics.FillRectangle(brush, rectangle);
  76. //}
  77. //else
  78. {
  79. int x = 0;
  80. int y = 0;
  81. switch (alignment)
  82. {
  83. case ContentAlignment.TopCenter:
  84. x = GetCenter(image.Width, rectangle.Width);
  85. break;
  86. case ContentAlignment.TopRight:
  87. x = (rectangle.Width % image.Width) - image.Width;
  88. break;
  89. case ContentAlignment.MiddleLeft:
  90. y = GetCenter(image.Height, rectangle.Height);
  91. break;
  92. case ContentAlignment.MiddleCenter:
  93. x = GetCenter(image.Width, rectangle.Width);
  94. y = GetCenter(image.Height, rectangle.Height);
  95. break;
  96. case ContentAlignment.MiddleRight:
  97. x = (rectangle.Width % image.Width) - image.Width;
  98. y = GetCenter(image.Height, rectangle.Height);
  99. break;
  100. case ContentAlignment.BottomLeft:
  101. y = (rectangle.Height % image.Height) - image.Height;
  102. break;
  103. case ContentAlignment.BottomCenter:
  104. x = GetCenter(image.Width, rectangle.Width);
  105. y = (rectangle.Height % image.Height) - image.Height;
  106. break;
  107. case ContentAlignment.BottomRight:
  108. x = (rectangle.Width % image.Width) - image.Width;
  109. y = (rectangle.Height % image.Height) - image.Height;
  110. break;
  111. default:
  112. break;
  113. }
  114. Rectangle rect = new Rectangle(0, 0,
  115. (x < 0 ? rectangle.Width - x : rectangle.Width),
  116. (y < 0 ? rectangle.Height - y : rectangle.Height));
  117. using (Bitmap tmp = new Bitmap(rect.Width, rect.Height))
  118. {
  119. using (Graphics tmpg = Graphics.FromImage(tmp))
  120. {
  121. SetGraphicsHighQuality(tmpg);
  122. tmpg.FillRectangle(brush, rect);
  123. Rectangle rectImage = new Rectangle(Math.Abs(x), Math.Abs(y),
  124. rectangle.Width,
  125. rectangle.Height);
  126. graphics.DrawImage(tmp, rectangle.X, rectangle.Y, rectImage, GraphicsUnit.Pixel);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. private static int GetCenter(int size1, int size2)
  133. {
  134. int t = (size2 - size1);
  135. if (t > 0)
  136. {
  137. return ((t / 2) % size1) - size1;
  138. }
  139. else if (t < 0)
  140. {
  141. return (t / 2);
  142. }
  143. return 0;
  144. }
  145. #endregion
  146. #region 拉伸
  147. /// <summary>
  148. /// 拉伸
  149. /// </summary>
  150. /// <param name="image"></param>
  151. /// <param name="size"></param>
  152. /// <returns></returns>
  153. public static Image Stretch(Image image, Size size)
  154. {
  155. //Image targetImage = new Bitmap(image, size);
  156. Image targetImage = new Bitmap(size.Width, size.Height);
  157. Stretch(image, targetImage);
  158. return targetImage;
  159. }
  160. /// <summary>
  161. /// 拉伸
  162. /// </summary>
  163. /// <param name="image"></param>
  164. /// <param name="targetImage"></param>
  165. /// <param name="rectangle"></param>
  166. public static void Stretch(Image image, Image targetImage, Rectangle? rectangle = null)
  167. {
  168. if (rectangle == null)
  169. {
  170. rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
  171. }
  172. else
  173. {
  174. rectangle = Rectangle.Intersect(rectangle.Value,
  175. new Rectangle(0, 0, targetImage.Width, targetImage.Height));
  176. if (rectangle.Value.IsEmpty)
  177. {
  178. return;
  179. }
  180. }
  181. using (Graphics graphics = Graphics.FromImage(targetImage))
  182. {
  183. Stretch(image, graphics, rectangle.Value);
  184. }
  185. }
  186. /// <summary>
  187. /// 拉伸
  188. /// </summary>
  189. /// <param name="image"></param>
  190. /// <param name="graphics"></param>
  191. /// <param name="rectangle"></param>
  192. public static void Stretch(Image image, Graphics graphics, Rectangle rectangle)
  193. {
  194. SetGraphicsHighQuality(graphics);
  195. graphics.DrawImage(image, rectangle);
  196. }
  197. #endregion
  198. #region 缩放(按比例)
  199. /// <summary>
  200. /// 缩放(按比例)类型
  201. /// </summary>
  202. public enum ZoomType
  203. {
  204. /// <summary>
  205. /// 自适应
  206. /// </summary>
  207. Adaptive = 1,
  208. /// <summary>
  209. /// 比例填充(超出部分居中)
  210. /// </summary>
  211. Filled,
  212. /// <summary>
  213. /// 按宽显示(超出部分居中)
  214. /// </summary>
  215. WidthRatio,
  216. /// <summary>
  217. /// 按高显示(超出部分居中)
  218. /// </summary>
  219. HeightRatio,
  220. }
  221. private static void ZoomWidth(Image image, Graphics graphics, Rectangle rectangle)
  222. {
  223. decimal ratio = rectangle.Width * 1m / image.Width;
  224. int height = (int)(image.Height * ratio);
  225. Rectangle rect = new Rectangle(0, 0, rectangle.Width, height);
  226. //using (Image tmp = Stretch(image, rect.Size))
  227. {
  228. SetGraphicsHighQuality(graphics);
  229. if (height > rectangle.Height)
  230. {
  231. rect.Width = image.Width;
  232. rect.Height = (int)(rectangle.Height / ratio);
  233. rect.Y = (image.Height - rect.Height) / 2;
  234. graphics.DrawImage(image, rectangle, rect, GraphicsUnit.Pixel);
  235. }
  236. else
  237. {
  238. rect.X = rectangle.X;
  239. rect.Y = rectangle.Y + (rectangle.Height - height) / 2;
  240. graphics.DrawImage(image, rect);
  241. }
  242. }
  243. }
  244. private static void ZoomHeight(Image image, Graphics graphics, Rectangle rectangle)
  245. {
  246. decimal ratio = rectangle.Height * 1m / image.Height;
  247. int width = (int)(image.Width * ratio);
  248. Rectangle rect = new Rectangle(0, 0, width, rectangle.Height);
  249. //using (Image tmp = Stretch(image, rect.Size))
  250. {
  251. SetGraphicsHighQuality(graphics);
  252. if (width > rectangle.Width)
  253. {
  254. rect.Height = image.Height;
  255. rect.Width = (int)(rectangle.Width / ratio);
  256. rect.X = (image.Width - rect.Width) / 2;
  257. graphics.DrawImage(image, rectangle, rect, GraphicsUnit.Pixel);
  258. }
  259. else
  260. {
  261. rect.X = rectangle.X + (rectangle.Width - width) / 2;
  262. rect.Y = rectangle.Y;
  263. graphics.DrawImage(image, rect);
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 缩放(按比例)
  269. /// </summary>
  270. /// <param name="image"></param>
  271. /// <param name="size"></param>
  272. /// <param name="type"></param>
  273. /// <returns></returns>
  274. public static Image Zoom(Image image, Size size, ZoomType type = ZoomType.Adaptive)
  275. {
  276. //Image targetImage = new Bitmap(image, size);
  277. Image targetImage = new Bitmap(size.Width, size.Height);
  278. Zoom(image, targetImage, null, type);
  279. return targetImage;
  280. }
  281. /// <summary>
  282. /// 缩放(按比例)
  283. /// </summary>
  284. /// <param name="image"></param>
  285. /// <param name="targetImage"></param>
  286. /// <param name="rectangle"></param>
  287. /// <param name="type"></param>
  288. public static void Zoom(Image image, Image targetImage, Rectangle? rectangle = null,
  289. ZoomType type = ZoomType.Adaptive)
  290. {
  291. if (rectangle == null)
  292. {
  293. rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
  294. }
  295. else
  296. {
  297. rectangle = Rectangle.Intersect(rectangle.Value,
  298. new Rectangle(0, 0, targetImage.Width, targetImage.Height));
  299. if (rectangle.Value.IsEmpty)
  300. {
  301. return;
  302. }
  303. }
  304. using (Graphics graphics = Graphics.FromImage(targetImage))
  305. {
  306. Zoom(image, graphics, rectangle.Value, type);
  307. }
  308. }
  309. /// <summary>
  310. /// 缩放(按比例)
  311. /// </summary>
  312. /// <param name="image"></param>
  313. /// <param name="graphics"></param>
  314. /// <param name="rectangle"></param>
  315. /// <param name="type"></param>
  316. public static void Zoom(Image image, Graphics graphics, Rectangle rectangle,
  317. ZoomType type = ZoomType.Adaptive)
  318. {
  319. switch (type)
  320. {
  321. case ZoomType.WidthRatio:
  322. ZoomWidth(image, graphics, rectangle);
  323. break;
  324. case ZoomType.HeightRatio:
  325. ZoomHeight(image, graphics, rectangle);
  326. break;
  327. default:
  328. decimal ratioHeight = rectangle.Height * 1m / image.Height;
  329. decimal ratioWidth = rectangle.Width * 1m / image.Width;
  330. if (ratioHeight != ratioWidth)
  331. {
  332. bool isWidth = ((ratioWidth > ratioHeight) == (type == ZoomType.Filled));
  333. if (isWidth)
  334. {
  335. ZoomWidth(image, graphics, rectangle);
  336. }
  337. else
  338. {
  339. ZoomHeight(image, graphics, rectangle);
  340. }
  341. }
  342. else
  343. {
  344. graphics.DrawImage(image, rectangle);
  345. }
  346. break;
  347. }
  348. }
  349. #endregion
  350. #region 剪裁
  351. /// <summary>
  352. /// 剪裁
  353. /// </summary>
  354. /// <param name="image"></param>
  355. /// <param name="rectangle"></param>
  356. /// <returns></returns>
  357. public static Image Cut(Image image, Rectangle rectangle)
  358. {
  359. Rectangle rectImage = new Rectangle(0, 0, image.Width, image.Height);
  360. Rectangle rect = Rectangle.Intersect(rectangle, rectImage);
  361. if (rect.IsEmpty)
  362. {
  363. return null;
  364. }
  365. //using (Bitmap tmp = new Bitmap(rectangle.Width, rectangle.Height))
  366. Bitmap tmp = new Bitmap(rectangle.Width, rectangle.Height);
  367. try
  368. {
  369. using (Graphics graphics = Graphics.FromImage(tmp))
  370. {
  371. SetGraphicsHighQuality(graphics);
  372. graphics.DrawImage(image,
  373. (rectangle.X < 0 ? -rectangle.X : 0),
  374. (rectangle.Y < 0 ? -rectangle.Y : 0),
  375. rect, GraphicsUnit.Pixel);
  376. }
  377. return tmp;
  378. }
  379. catch
  380. {
  381. tmp.Dispose();
  382. throw;
  383. }
  384. }
  385. #endregion
  386. #region 显示
  387. // 水平 左 中 右
  388. // 垂直 上 中 下
  389. /// <summary>
  390. /// 显示
  391. /// </summary>
  392. /// <param name="image"></param>
  393. /// <param name="size"></param>
  394. /// <param name="alignment"></param>
  395. /// <param name="cut"></param>
  396. /// <returns></returns>
  397. public static Image Show(Image image, Size size,
  398. ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
  399. {
  400. Image targetImage = new Bitmap(size.Width, size.Height);
  401. Show(image, targetImage, null, alignment, cut);
  402. return targetImage;
  403. }
  404. /// <summary>
  405. /// 显示
  406. /// </summary>
  407. /// <param name="image"></param>
  408. /// <param name="targetImage"></param>
  409. /// <param name="rectangle"></param>
  410. /// <param name="alignment"></param>
  411. /// <param name="cut"></param>
  412. public static void Show(Image image, Image targetImage, Rectangle? rectangle = null,
  413. ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
  414. {
  415. if (rectangle == null)
  416. {
  417. rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
  418. }
  419. else
  420. {
  421. rectangle = Rectangle.Intersect(rectangle.Value,
  422. new Rectangle(0, 0, targetImage.Width, targetImage.Height));
  423. if (rectangle.Value.IsEmpty)
  424. {
  425. return;
  426. }
  427. }
  428. using (Graphics graphics = Graphics.FromImage(targetImage))
  429. {
  430. Show(image, graphics, rectangle.Value, alignment, cut);
  431. }
  432. }
  433. /// <summary>
  434. /// 显示
  435. /// </summary>
  436. /// <param name="image"></param>
  437. /// <param name="graphics"></param>
  438. /// <param name="rectangle"></param>
  439. /// <param name="alignment"></param>
  440. /// <param name="cut"></param>
  441. public static void Show(Image image, Graphics graphics, Rectangle rectangle,
  442. ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
  443. {
  444. int x = 0;
  445. int y = 0;
  446. switch (alignment)
  447. {
  448. case ContentAlignment.TopLeft:
  449. break;
  450. case ContentAlignment.TopCenter:
  451. x = (rectangle.Width - image.Width) / 2;
  452. break;
  453. case ContentAlignment.TopRight:
  454. x = (rectangle.Width - image.Width);
  455. break;
  456. case ContentAlignment.MiddleLeft:
  457. y = (rectangle.Height - image.Height) / 2;
  458. break;
  459. case ContentAlignment.MiddleCenter:
  460. x = (rectangle.Width - image.Width) / 2;
  461. y = (rectangle.Height - image.Height) / 2;
  462. break;
  463. case ContentAlignment.MiddleRight:
  464. x = (rectangle.Width - image.Width);
  465. y = (rectangle.Height - image.Height) / 2;
  466. break;
  467. case ContentAlignment.BottomLeft:
  468. y = (rectangle.Height - image.Height);
  469. break;
  470. case ContentAlignment.BottomCenter:
  471. x = (rectangle.Width - image.Width) / 2;
  472. y = (rectangle.Height - image.Height);
  473. break;
  474. case ContentAlignment.BottomRight:
  475. x = (rectangle.Width - image.Width);
  476. y = (rectangle.Height - image.Height);
  477. break;
  478. default:
  479. break;
  480. }
  481. Rectangle rectIamge = new Rectangle(0, 0, image.Width, image.Height);
  482. if (cut)
  483. {
  484. Rectangle rect = new Rectangle(-x, -y, rectangle.Width, rectangle.Height);
  485. rectIamge = Rectangle.Intersect(rectIamge, rect);
  486. if (x > 0)
  487. {
  488. x += rectangle.X;
  489. }
  490. else
  491. {
  492. x = rectangle.X;
  493. }
  494. if (y > 0)
  495. {
  496. y += rectangle.Y;
  497. }
  498. else
  499. {
  500. y = rectangle.Y;
  501. }
  502. }
  503. else
  504. {
  505. x += rectangle.X;
  506. y += rectangle.Y;
  507. }
  508. SetGraphicsHighQuality(graphics);
  509. graphics.DrawImage(image, x, y, rectIamge, GraphicsUnit.Pixel);
  510. }
  511. #endregion
  512. #region 压缩
  513. /// <summary>
  514. /// 压缩
  515. /// </summary>
  516. /// <param name="image"></param>
  517. /// <param name="quality"></param>
  518. /// <param name="size"></param>
  519. /// <returns></returns>
  520. public static Stream ZipImage(Image image, long quality, Size? size = null)
  521. {
  522. bool isZoom = true;
  523. if (size == null)
  524. {
  525. isZoom = false;
  526. size = image.Size;
  527. }
  528. Image bitmap = image;
  529. if (isZoom)
  530. {
  531. using (Bitmap tmp = new Bitmap(size.Value.Width, size.Value.Height))
  532. {
  533. Stretch(image, tmp);
  534. bitmap = tmp;
  535. }
  536. }
  537. else
  538. {
  539. }
  540. using (EncoderParameters myEncoderParameters = new EncoderParameters(1))
  541. {
  542. using (EncoderParameter myEncoderParameter = new EncoderParameter(Encoder.Quality, quality))
  543. {
  544. myEncoderParameters.Param[0] = myEncoderParameter;
  545. ImageCodecInfo codecInfo = GetEncoderInfo(image.RawFormat.ToString());
  546. MemoryStream ms = new MemoryStream();
  547. bitmap.Save(ms, codecInfo, myEncoderParameters);
  548. return ms;
  549. }
  550. }
  551. }
  552. /// <summary>
  553. /// 获取图片编码信息
  554. /// </summary>
  555. private static ImageCodecInfo GetEncoderInfo(string mimeType)
  556. {
  557. foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
  558. {
  559. if (encoder.MimeType == mimeType)
  560. {
  561. return encoder;
  562. }
  563. }
  564. return null;
  565. }
  566. #endregion
  567. #region 设置透明
  568. /// <summary>
  569. /// 设置透明
  570. /// </summary>
  571. /// <param name="graphics"></param>
  572. public static void MakeTransparent(Graphics graphics)
  573. {
  574. graphics.Clear(Color.Transparent);
  575. }
  576. /// <summary>
  577. /// 设置透明
  578. /// </summary>
  579. /// <param name="image"></param>
  580. public static void MakeTransparent(Bitmap image)
  581. {
  582. image.MakeTransparent();
  583. }
  584. /// <summary>
  585. /// Returns a transparent background GIF image from the specified Bitmap.
  586. /// </summary>
  587. /// <param name="bitmap">The Bitmap to make transparent.</param>
  588. /// <param name="color">The Color to make transparent.</param>
  589. /// <returns>New Bitmap containing a transparent background gif.</returns>
  590. public static Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
  591. {
  592. byte R = color.R;
  593. byte G = color.G;
  594. byte B = color.B;
  595. using (MemoryStream fin = new MemoryStream())
  596. {
  597. bitmap.Save(fin, ImageFormat.Gif);
  598. using (MemoryStream fout = new MemoryStream((int)fin.Length))
  599. {
  600. byte[] buf = new byte[256];
  601. byte transparentIdx = 0;
  602. fin.Seek(0, SeekOrigin.Begin);
  603. //header
  604. int count = fin.Read(buf, 0, 13);
  605. if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70))
  606. {
  607. return null; //GIF
  608. }
  609. fout.Write(buf, 0, 13);
  610. int i = 0;
  611. if ((buf[10] & 0x80) > 0)
  612. {
  613. i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
  614. }
  615. for (; i != 0; i--)
  616. {
  617. fin.Read(buf, 0, 3);
  618. if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
  619. {
  620. transparentIdx = (byte)(256 - i);
  621. }
  622. fout.Write(buf, 0, 3);
  623. }
  624. while (true)
  625. {
  626. fin.Read(buf, 0, 1);
  627. fout.Write(buf, 0, 1);
  628. if (buf[0] != 0x21)
  629. {
  630. break;
  631. }
  632. fin.Read(buf, 0, 1);
  633. fout.Write(buf, 0, 1);
  634. bool gcePresent = (buf[0] == 0xf9);
  635. while (true)
  636. {
  637. fin.Read(buf, 0, 1);
  638. fout.Write(buf, 0, 1);
  639. if (buf[0] == 0)
  640. {
  641. break;
  642. }
  643. count = buf[0];
  644. if (fin.Read(buf, 0, count) != count)
  645. {
  646. return null;
  647. }
  648. if (gcePresent)
  649. {
  650. if (count == 4)
  651. {
  652. buf[0] |= 0x01;
  653. buf[3] = transparentIdx;
  654. }
  655. }
  656. fout.Write(buf, 0, count);
  657. }
  658. }
  659. while (count > 0)
  660. {
  661. count = fin.Read(buf, 0, 1);
  662. fout.Write(buf, 0, 1);
  663. }
  664. fin.Close();
  665. fout.Flush();
  666. return new Bitmap(fout);
  667. }
  668. }
  669. }
  670. #endregion
  671. /// <summary>
  672. /// 按像素放大整数倍
  673. /// </summary>
  674. /// <param name="image"></param>
  675. /// <param name="ratioWidth"></param>
  676. /// <param name="ratioHeight"></param>
  677. /// <returns></returns>
  678. public static Bitmap ZoomPixel(Bitmap image, int ratioWidth, int ratioHeight)
  679. {
  680. if (image == null || ratioWidth < 1 || ratioHeight < 1)
  681. {
  682. return image;
  683. }
  684. Bitmap targetImage = new Bitmap(image.Width * ratioWidth, image.Height * ratioHeight);
  685. BitmapData targetImageData = null;
  686. try
  687. {
  688. BitmapData imageData =
  689. image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
  690. ImageLockMode.ReadOnly,
  691. PixelFormat.Format32bppArgb);
  692. targetImageData =
  693. targetImage.LockBits(new Rectangle(0, 0, targetImage.Width, targetImage.Height),
  694. ImageLockMode.WriteOnly,
  695. PixelFormat.Format32bppArgb);
  696. byte[] imagePixels = new byte[imageData.Stride * imageData.Height];
  697. Marshal.Copy(imageData.Scan0, imagePixels, 0, imagePixels.Length);
  698. byte[] targetImagePixels = new byte[targetImageData.Stride * targetImageData.Height];
  699. int targetImageIndex = 0;
  700. for (int y = 0; y < image.Height; y++)
  701. {
  702. for (int rh = 0; rh < ratioHeight; rh++)
  703. {
  704. for (int x = 0; x < image.Width; x++)
  705. {
  706. for (int rw = 0; rw < ratioWidth; rw++)
  707. {
  708. int i = y * imageData.Stride + x * 4;
  709. targetImagePixels[targetImageIndex++] = imagePixels[i++];
  710. targetImagePixels[targetImageIndex++] = imagePixels[i++];
  711. targetImagePixels[targetImageIndex++] = imagePixels[i++];
  712. targetImagePixels[targetImageIndex++] = imagePixels[i++];
  713. }
  714. }
  715. }
  716. }
  717. Marshal.Copy(targetImagePixels, 0, targetImageData.Scan0, targetImagePixels.Length);
  718. }
  719. finally
  720. {
  721. targetImage.UnlockBits(targetImageData);
  722. }
  723. return targetImage;
  724. }
  725. /*
  726. // 对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定..尤其是在读取图片后,生成缩略图之后,文件会被压缩而失真..
  727. //主要原因是因为没有重新设置Graphics的几个属性..
  728. //1.Graphics.SmoothingMode属性: 例如SmoothingMode.HighQuality可以产生高质量图片,但是效率低.
  729. //2.Graphics.CompositingQuality 属性: 例如:CompositingQuality.HighQuality也是产生高质量图,效率低下.
  730. //3.Graphics.InterpolationMode 属性, 例如:InterpolationMode.HighQualityBicubic与前两个也是同样的效果.
  731. // 这三个属性的值都是Enum,具体的Enum参数可以查看MSDN的说明,在这里就我不多说了,
  732. //如果是对图片进行放大,缩小,可以调整Graphics.CompositingQuality 和Graphics.InterpolationMode 两个属性,如果是图片生成, 则可以调整Graphics.SmoothingMode属性
  733. //另外一个问题就是关于文字生成的..按照正常的模式生成的文字, 可以很明显的看到文字带有锯齿..解决的办法也是需要修改Graphics的一个属性: Graphics.TextRenderingHint注意一点, 修改TextRenderingHint的话, 需要引入System.Drawing.Text, 例如:Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  734. // 经过对这四个属性的修改,操作大部分的图片之后,产生的结果都是比较让人满意
  735. */
  736. /// <summary>
  737. ///
  738. /// </summary>
  739. /// <param name="graphics"></param>
  740. public static void SetGraphicsHighQuality(Graphics graphics)
  741. {
  742. // 设置绘图质量
  743. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  744. graphics.CompositingQuality = CompositingQuality.HighQuality;
  745. graphics.SmoothingMode = SmoothingMode.HighQuality;
  746. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  747. graphics.CompositingMode = CompositingMode.SourceOver;
  748. //graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  749. }
  750. }
  751. }