ImageUtils.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. using InABox.Core;
  2. using Syncfusion.Pdf.Parsing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Windows;
  9. using System.Windows.Interop;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Xml.Linq;
  13. using ColorHelper;
  14. using Color = System.Drawing.Color;
  15. using Pen = System.Drawing.Pen;
  16. using PixelFormat = System.Drawing.Imaging.PixelFormat;
  17. using Point = System.Drawing.Point;
  18. using Size = System.Drawing.Size;
  19. using System.Windows.Controls;
  20. using System.Drawing;
  21. using System.Collections.Generic;
  22. using System;
  23. using System.Linq;
  24. using Syncfusion.Pdf.Graphics;
  25. using Syncfusion.Pdf;
  26. using System.Diagnostics.CodeAnalysis;
  27. using System.Reflection;
  28. using System.Runtime;
  29. using System.Text;
  30. using SharpVectors.Converters;
  31. using SharpVectors.Renderers.Wpf;
  32. using Syncfusion.PdfToImageConverter;
  33. using Encoder = System.Drawing.Imaging.Encoder;
  34. namespace InABox.WPF
  35. {
  36. public static class ImageUtils
  37. {
  38. // https://en.wikipedia.org/wiki/List_of_file_signatures
  39. /* Bytes in c# have a range of 0 to 255 so each byte can be represented as
  40. * a two digit hex string. */
  41. private static readonly Dictionary<ImageFormat, string[][]> SignatureTable = new()
  42. {
  43. {
  44. ImageFormat.Jpeg,
  45. new[]
  46. {
  47. new[] { "FF", "D8", "FF", "DB" },
  48. new[] { "FF", "D8", "FF", "EE" },
  49. new[] { "FF", "D8", "FF", "E0", "00", "10", "4A", "46", "49", "46", "00", "01" }
  50. }
  51. },
  52. {
  53. ImageFormat.Gif,
  54. new[]
  55. {
  56. new[] { "47", "49", "46", "38", "37", "61" },
  57. new[] { "47", "49", "46", "38", "39", "61" }
  58. }
  59. },
  60. {
  61. ImageFormat.Png,
  62. new[]
  63. {
  64. new[] { "89", "50", "4E", "47", "0D", "0A", "1A", "0A" }
  65. }
  66. },
  67. {
  68. ImageFormat.Bmp,
  69. new[]
  70. {
  71. new[] { "42", "4D" }
  72. }
  73. }
  74. };
  75. public static Size Adjust(this Size src, double maxWidth, double maxHeight, bool enlarge = false)
  76. {
  77. maxWidth = enlarge ? maxWidth : Math.Min(maxWidth, src.Width);
  78. maxHeight = enlarge ? maxHeight : Math.Min(maxHeight, src.Height);
  79. var rnd = Math.Min((decimal)maxWidth / src.Width, (decimal)maxHeight / src.Height);
  80. return new Size((int)Math.Round(src.Width * rnd), (int)Math.Round(src.Height * rnd));
  81. }
  82. public static Bitmap AsGrayScale(this Bitmap source)
  83. {
  84. //create a blank bitmap the same size as original
  85. var newBitmap = new Bitmap(source.Width, source.Height);
  86. //get a graphics object from the new image
  87. var g = Graphics.FromImage(newBitmap);
  88. //create the grayscale ColorMatrix
  89. var colorMatrix = new ColorMatrix(
  90. new[]
  91. {
  92. new[] { .3f, .3f, .3f, 0, 0 },
  93. new[] { .59f, .59f, .59f, 0, 0 },
  94. new[] { .11f, .11f, .11f, 0, 0 },
  95. new float[] { 0, 0, 0, 1, 0 },
  96. new float[] { 0, 0, 0, 0, 1 }
  97. });
  98. //create some image attributes
  99. var attributes = new ImageAttributes();
  100. //set the color matrix attribute
  101. attributes.SetColorMatrix(colorMatrix);
  102. //draw the original image on the new image
  103. //using the grayscale color matrix
  104. g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
  105. 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
  106. //dispose the Graphics object
  107. g.Dispose();
  108. return newBitmap;
  109. }
  110. public static BitmapImage AsBitmapImage(this Bitmap src, int height, int width, bool transparent = true)
  111. {
  112. var resized = new Bitmap(src, new Size(width, height));
  113. return AsBitmapImage(resized, transparent);
  114. }
  115. public static BitmapImage AsBitmapImage(this Bitmap src, Color transparent)
  116. {
  117. src.MakeTransparent(transparent);
  118. return src.AsBitmapImage();
  119. }
  120. public static Bitmap ChangeColor(this Bitmap image, Color fromColor, Color toColor)
  121. {
  122. var attributes = new ImageAttributes();
  123. attributes.SetRemapTable(new ColorMap[]
  124. {
  125. new()
  126. {
  127. OldColor = fromColor,
  128. NewColor = toColor
  129. }
  130. }, ColorAdjustType.Bitmap);
  131. using (var g = Graphics.FromImage(image))
  132. {
  133. g.DrawImage(
  134. image,
  135. new Rectangle(Point.Empty, image.Size),
  136. 0, 0, image.Width, image.Height,
  137. GraphicsUnit.Pixel,
  138. attributes);
  139. }
  140. return image;
  141. }
  142. public static Bitmap Fade(this Bitmap source, float opacity)
  143. {
  144. var result = new Bitmap(source.Width, source.Height);
  145. //create a graphics object from the image
  146. using (var gfx = Graphics.FromImage(result))
  147. {
  148. if (opacity < 1.0)
  149. gfx.Clear(Color.White);
  150. //create a color matrix object
  151. var matrix = new ColorMatrix();
  152. //set the opacity
  153. matrix.Matrix33 = opacity;
  154. //create image attributes
  155. var attributes = new ImageAttributes();
  156. //set the color(opacity) of the image
  157. attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  158. //now draw the image
  159. gfx.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel,
  160. attributes);
  161. }
  162. return result;
  163. }
  164. public static BitmapImage AsBitmapImage(this Bitmap src, Color replace, Color with)
  165. {
  166. return src.ChangeColor(replace, with).AsBitmapImage(false);
  167. }
  168. public static Bitmap AsBitmap(this BitmapImage bitmapImage)
  169. {
  170. using (var outStream = new MemoryStream())
  171. {
  172. BitmapEncoder enc = new BmpBitmapEncoder();
  173. enc.Frames.Add(BitmapFrame.Create(bitmapImage));
  174. enc.Save(outStream);
  175. var bitmap = new Bitmap(outStream);
  176. return new Bitmap(bitmap);
  177. }
  178. }
  179. public static Bitmap AsBitmap(this BitmapSource source)
  180. {
  181. var width = source.PixelWidth;
  182. var height = source.PixelHeight;
  183. var stride = width * ((source.Format.BitsPerPixel + 7) / 8);
  184. var ptr = IntPtr.Zero;
  185. try
  186. {
  187. ptr = Marshal.AllocHGlobal(height * stride);
  188. source.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride);
  189. using (var btm = new Bitmap(width, height, stride, PixelFormat.Format1bppIndexed, ptr))
  190. {
  191. return new Bitmap(btm);
  192. }
  193. }
  194. finally
  195. {
  196. if (ptr != IntPtr.Zero)
  197. Marshal.FreeHGlobal(ptr);
  198. }
  199. }
  200. public static Bitmap AsBitmap2(this BitmapSource source)
  201. {
  202. var bmp = new Bitmap(
  203. source.PixelWidth,
  204. source.PixelHeight,
  205. PixelFormat.Format32bppPArgb);
  206. var data = bmp.LockBits(
  207. new Rectangle(Point.Empty, bmp.Size),
  208. ImageLockMode.WriteOnly,
  209. PixelFormat.Format32bppPArgb);
  210. source.CopyPixels(
  211. Int32Rect.Empty,
  212. data.Scan0,
  213. data.Height * data.Stride,
  214. data.Stride);
  215. bmp.UnlockBits(data);
  216. return bmp;
  217. }
  218. public static BitmapImage? BitmapImageFromBase64(string base64)
  219. {
  220. return BitmapImageFromBytes(Convert.FromBase64String(base64));
  221. }
  222. public static BitmapImage? BitmapImageFromBytes(byte[]? data)
  223. {
  224. if (data?.Any() != true)
  225. return null;
  226. var imageSource = new BitmapImage();
  227. using (var ms = new MemoryStream(data))
  228. {
  229. imageSource.BeginInit();
  230. imageSource.StreamSource = ms;
  231. imageSource.CacheOption = BitmapCacheOption.OnLoad;
  232. imageSource.EndInit();
  233. }
  234. return imageSource;
  235. }
  236. public static BitmapImage? BitmapImageFromStream(Stream data)
  237. {
  238. var imageSource = new BitmapImage();
  239. imageSource.BeginInit();
  240. imageSource.StreamSource = data;
  241. imageSource.CacheOption = BitmapCacheOption.OnLoad;
  242. imageSource.EndInit();
  243. return imageSource;
  244. }
  245. public static BitmapImage AsBitmapImage(this Bitmap src, bool transparent = true)
  246. {
  247. if (transparent)
  248. {
  249. var pixel = src.GetPixel(0, 0);
  250. if(pixel.A == byte.MaxValue)
  251. {
  252. src.MakeTransparent(pixel);
  253. }
  254. }
  255. var bitmapImage = new BitmapImage();
  256. using (var memory = new MemoryStream())
  257. {
  258. src.Save(memory, ImageFormat.Png);
  259. memory.Position = 0;
  260. bitmapImage.BeginInit();
  261. bitmapImage.StreamSource = memory;
  262. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  263. bitmapImage.EndInit();
  264. }
  265. return bitmapImage;
  266. }
  267. public static BitmapSource AsBitmapSource(this Metafile metafile, int width, int height, Color background)
  268. {
  269. var src = new Bitmap(metafile.Width, metafile.Height);
  270. src.SetResolution(metafile.HorizontalResolution, metafile.VerticalResolution);
  271. using (var g = Graphics.FromImage(src))
  272. {
  273. g.DrawImage(metafile, 0, 0, metafile.Width, metafile.Height);
  274. }
  275. var scale = Math.Min(width / (float)metafile.Width, height / (float)metafile.Height);
  276. var scaleWidth = src.Width * scale;
  277. var scaleHeight = src.Height * scale;
  278. var xoffset = (width - scaleWidth) / 2.0F;
  279. var yoffset = (height - scaleHeight) / 2.0F;
  280. using (var bmp = new Bitmap(width, height))
  281. {
  282. bmp.SetResolution(metafile.HorizontalResolution, metafile.VerticalResolution);
  283. using (var g = Graphics.FromImage(bmp))
  284. {
  285. g.InterpolationMode = InterpolationMode.High;
  286. g.CompositingQuality = CompositingQuality.HighQuality;
  287. g.SmoothingMode = SmoothingMode.AntiAlias;
  288. g.FillRectangle(new SolidBrush(background), new RectangleF(0, 0, width, height));
  289. g.DrawImage(src, xoffset, yoffset, scaleWidth, scaleHeight);
  290. }
  291. bmp.Save("c:\\development\\emf2bmp.png");
  292. return Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  293. }
  294. }
  295. public static Bitmap BitmapSourceToBitmap(BitmapSource source)
  296. {
  297. if (source == null)
  298. return null;
  299. var pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb; //Bgr32 equiv default
  300. if (source.Format == PixelFormats.Bgr24)
  301. pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
  302. else if (source.Format == PixelFormats.Pbgra32)
  303. pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
  304. else if (source.Format == PixelFormats.Prgba64)
  305. pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppPArgb;
  306. Bitmap bmp = new Bitmap(
  307. source.PixelWidth,
  308. source.PixelHeight,
  309. pixelFormat);
  310. BitmapData data = bmp.LockBits(
  311. new Rectangle(System.Drawing.Point.Empty, bmp.Size),
  312. ImageLockMode.WriteOnly,
  313. pixelFormat);
  314. source.CopyPixels(
  315. Int32Rect.Empty,
  316. data.Scan0,
  317. data.Height * data.Stride,
  318. data.Stride);
  319. bmp.UnlockBits(data);
  320. return bmp;
  321. }
  322. public static BitmapImage ToBitmapImage(this Bitmap bitmap)
  323. {
  324. using (var memory = new MemoryStream())
  325. {
  326. bitmap.Save(memory, ImageFormat.Png);
  327. memory.Position = 0;
  328. var bitmapImage = new BitmapImage();
  329. bitmapImage.BeginInit();
  330. bitmapImage.StreamSource = memory;
  331. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  332. bitmapImage.EndInit();
  333. bitmapImage.Freeze();
  334. return bitmapImage;
  335. }
  336. }
  337. /// <summary>
  338. /// Load an image from image data; this will return <see langword="null"/> if <paramref name="imageData"/> is <see langword="null"/> or empty.
  339. /// </summary>
  340. /// <param name="imageData"></param>
  341. /// <returns></returns>
  342. public static BitmapImage? LoadImage(byte[]? imageData)
  343. {
  344. if(imageData is null || imageData.Length == 0)
  345. {
  346. return null;
  347. }
  348. var result = new BitmapImage();
  349. result.LoadImage(imageData);
  350. return result;
  351. }
  352. private static void LoadImage(this BitmapImage image, byte[] imageData)
  353. {
  354. using (var mem = new MemoryStream(imageData))
  355. {
  356. mem.Position = 0;
  357. image.BeginInit();
  358. image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
  359. image.CacheOption = BitmapCacheOption.OnLoad;
  360. image.UriSource = null;
  361. image.StreamSource = mem;
  362. image.EndInit();
  363. }
  364. image.Freeze();
  365. }
  366. public static Bitmap? MergeBitmaps(IEnumerable<Bitmap> bitmaps, int padding)
  367. {
  368. if (!bitmaps.Any())
  369. return null;
  370. var totalwidth = bitmaps.Aggregate(0, (total, next) => total + next.Width + (total > 0 ? padding : 0) );
  371. var maxheight = bitmaps.Aggregate(0, (max, next) => Math.Max(next.Height,max) );
  372. Bitmap result = new Bitmap(totalwidth, maxheight);
  373. using (Graphics g = Graphics.FromImage(result))
  374. {
  375. g.Clear(Color.Transparent);
  376. int left = 0;
  377. foreach (var bitmap in bitmaps)
  378. {
  379. g.DrawImage(bitmap, left, 0);
  380. left += bitmap.Width + padding;
  381. }
  382. }
  383. return result;
  384. }
  385. public static byte[] ToArray<T>(this BitmapImage image) where T : BitmapEncoder, new()
  386. {
  387. byte[] data;
  388. var encoder = new T();
  389. encoder.Frames.Add(BitmapFrame.Create(image));
  390. using (var ms = new MemoryStream())
  391. {
  392. encoder.Save(ms);
  393. data = ms.ToArray();
  394. }
  395. return data;
  396. }
  397. public static BitmapImage Resize(this BitmapImage image, int height, int width)
  398. {
  399. var buffer = image.ToArray<BmpBitmapEncoder>();
  400. var ms = new MemoryStream(buffer);
  401. var result = new BitmapImage();
  402. result.BeginInit();
  403. result.CacheOption = BitmapCacheOption.None;
  404. result.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
  405. result.DecodePixelWidth = width;
  406. result.DecodePixelHeight = height;
  407. result.StreamSource = ms;
  408. result.Rotation = Rotation.Rotate0;
  409. result.EndInit();
  410. buffer = null;
  411. return result;
  412. }
  413. public static Bitmap Resize(this Bitmap bitmap, int width, int height)
  414. {
  415. if ((width == bitmap.Width) && (height == bitmap.Height))
  416. return bitmap;
  417. return new Bitmap(bitmap,new Size(width,height));
  418. }
  419. public static BitmapImage Scale(this BitmapImage image, int maxheight, int maxwidth)
  420. {
  421. var scaleHeight = maxheight / (float)image.Height;
  422. var scaleWidth = maxwidth / (float)image.Width;
  423. var scale = Math.Min(scaleHeight, scaleWidth);
  424. return image.Resize((int)(image.Height * scale), (int)(image.Width * scale));
  425. }
  426. public static Bitmap BitmapFromColor(Color color, int width, int height, Color frame)
  427. {
  428. var result = new Bitmap(width, height);
  429. var g = Graphics.FromImage(result);
  430. g.Clear(color);
  431. if (frame != Color.Transparent)
  432. g.DrawRectangle(new Pen(new SolidBrush(frame), 1), new Rectangle(0, 0, width-1, height-1));
  433. return result;
  434. }
  435. public static Bitmap BitmapFromColor(System.Windows.Media.Color color, int width, int height, System.Windows.Media.Color frame)
  436. {
  437. var result = new Bitmap(width, height);
  438. var g = Graphics.FromImage(result);
  439. g.Clear(Color.FromArgb(color.A,color.R,color.G,color.B));
  440. if (frame != Colors.Transparent)
  441. g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(frame.A,frame.R,frame.G,frame.B)), 1F), new Rectangle(0, 0, width-1, height-1));
  442. return result;
  443. }
  444. public static Color MixColors(this Color color1, double factor, Color color2)
  445. {
  446. if (factor < 0) throw new Exception($"Factor {factor} must be >= 0.");
  447. if (factor > 1) throw new Exception($"Factor {factor} must be <= 1.");
  448. if (factor == 0) return color2;
  449. if (factor == 1) return color1;
  450. var factor1 = 1 - factor;
  451. return Color.FromArgb(
  452. (byte)(color1.A * factor + color2.A * factor1),
  453. (byte)(color1.R * factor + color2.R * factor1),
  454. (byte)(color1.G * factor + color2.G * factor1),
  455. (byte)(color1.B * factor + color2.B * factor1));
  456. }
  457. public static System.Windows.Media.Color MixColors(this System.Windows.Media.Color color1, double factor, System.Windows.Media.Color color2)
  458. {
  459. if (factor < 0) throw new Exception($"Factor {factor} must be >= 0.");
  460. if (factor > 1) throw new Exception($"Factor {factor} must be <= 1.");
  461. if (factor == 0) return color2;
  462. if (factor == 1) return color1;
  463. var factor1 = 1 - factor;
  464. return System.Windows.Media.Color.FromArgb(
  465. (byte)(color1.A * factor + color2.A * factor1),
  466. (byte)(color1.R * factor + color2.R * factor1),
  467. (byte)(color1.G * factor + color2.G * factor1),
  468. (byte)(color1.B * factor + color2.B * factor1));
  469. }
  470. public static string ColorToString(Color color)
  471. {
  472. return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}",
  473. color.A,
  474. color.R,
  475. color.G,
  476. color.B
  477. );
  478. }
  479. public static Color StringToColor(string colorcode)
  480. {
  481. var col = Color.Transparent;
  482. if (!string.IsNullOrEmpty(colorcode))
  483. {
  484. var code = colorcode.Replace("#", "");
  485. if (code.Length == 6)
  486. col = Color.FromArgb(255,
  487. byte.Parse(code.Substring(0, 2), NumberStyles.HexNumber),
  488. byte.Parse(code.Substring(2, 2), NumberStyles.HexNumber),
  489. byte.Parse(code.Substring(4, 2), NumberStyles.HexNumber));
  490. else if (code.Length == 8)
  491. col = Color.FromArgb(
  492. byte.Parse(code.Substring(0, 2), NumberStyles.HexNumber),
  493. byte.Parse(code.Substring(2, 2), NumberStyles.HexNumber),
  494. byte.Parse(code.Substring(4, 2), NumberStyles.HexNumber),
  495. byte.Parse(code.Substring(6, 2), NumberStyles.HexNumber));
  496. }
  497. return col;
  498. }
  499. public static System.Windows.Media.Color StringToMediaColor(string colorcode)
  500. {
  501. var col = Colors.Transparent;
  502. if (!string.IsNullOrEmpty(colorcode))
  503. {
  504. var code = colorcode.Replace("#", "");
  505. if (code.Length == 6)
  506. col = System.Windows.Media.Color.FromArgb(255,
  507. byte.Parse(code.Substring(0, 2), NumberStyles.HexNumber),
  508. byte.Parse(code.Substring(2, 2), NumberStyles.HexNumber),
  509. byte.Parse(code.Substring(4, 2), NumberStyles.HexNumber));
  510. else if (code.Length == 8)
  511. col = System.Windows.Media.Color.FromArgb(
  512. byte.Parse(code.Substring(0, 2), NumberStyles.HexNumber),
  513. byte.Parse(code.Substring(2, 2), NumberStyles.HexNumber),
  514. byte.Parse(code.Substring(4, 2), NumberStyles.HexNumber),
  515. byte.Parse(code.Substring(6, 2), NumberStyles.HexNumber));
  516. }
  517. return col;
  518. }
  519. /// <summary>
  520. /// Creates color with corrected brightness.
  521. /// </summary>
  522. /// <param name="color">Color to correct.</param>
  523. /// <param name="correctionFactor">
  524. /// The brightness correction factor. Must be between -1 and 1.
  525. /// Negative values produce darker colors.
  526. /// </param>
  527. /// <returns>
  528. /// Corrected <see cref="Color" /> structure.
  529. /// </returns>
  530. public static System.Windows.Media.Color AdjustBrightness(this System.Windows.Media.Color color, float correctionFactor)
  531. {
  532. float red = color.R;
  533. float green = color.G;
  534. float blue = color.B;
  535. if (correctionFactor < 0)
  536. {
  537. correctionFactor = 1 + correctionFactor;
  538. red *= correctionFactor;
  539. green *= correctionFactor;
  540. blue *= correctionFactor;
  541. }
  542. else
  543. {
  544. red = (255 - red) * correctionFactor + red;
  545. green = (255 - green) * correctionFactor + green;
  546. blue = (255 - blue) * correctionFactor + blue;
  547. }
  548. return System.Windows.Media.Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
  549. }
  550. public static bool TryGetImageType(byte[] imageData, [NotNullWhen(true)] out ImageFormat? format)
  551. {
  552. foreach (var signatureEntry in SignatureTable)
  553. foreach (var signature in signatureEntry.Value)
  554. {
  555. var isMatch = true;
  556. for (var i = 0; i < signature.Length; i++)
  557. {
  558. var signatureByte = signature[i];
  559. // ToString("X") gets the hex representation and pads it to always be length 2
  560. var imageByte = imageData[i]
  561. .ToString("X2");
  562. if (signatureByte == imageByte)
  563. continue;
  564. isMatch = false;
  565. break;
  566. }
  567. if (isMatch)
  568. {
  569. format = signatureEntry.Key;
  570. return true;
  571. }
  572. }
  573. format = null;
  574. return false;
  575. }
  576. /// <summary>
  577. /// Takes a byte array and determines the image file type by
  578. /// comparing the first few bytes of the file to a list of known
  579. /// image file signatures.
  580. /// </summary>
  581. /// <param name="imageData">Byte array of the image data</param>
  582. /// <returns>ImageFormat corresponding to the image file format</returns>
  583. /// <exception cref="ArgumentException">Thrown if the image type can't be determined</exception>
  584. public static ImageFormat GetImageType(byte[] imageData)
  585. {
  586. if(TryGetImageType(imageData, out var format))
  587. {
  588. return format;
  589. }
  590. throw new ArgumentException("The byte array did not match any known image file signatures.");
  591. }
  592. public static System.Drawing.Bitmap Invert(this System.Drawing.Bitmap source)
  593. {
  594. Bitmap bmpDest = new Bitmap(source.Width,source.Height);
  595. ColorMatrix clrMatrix = new ColorMatrix(new float[][]
  596. {
  597. new float[] {-1, 0, 0, 0, 0},
  598. new float[] {0, -1, 0, 0, 0},
  599. new float[] {0, 0, -1, 0, 0},
  600. new float[] {0, 0, 0, 1, 0},
  601. new float[] {1, 1, 1, 0, 1}
  602. });
  603. using (ImageAttributes attrImage = new ImageAttributes())
  604. {
  605. attrImage.SetColorMatrix(clrMatrix);
  606. using (Graphics g = Graphics.FromImage(bmpDest))
  607. {
  608. g.DrawImage(source, new Rectangle(0, 0,
  609. source.Width, source.Height), 0, 0,
  610. source.Width, source.Height, GraphicsUnit.Pixel,
  611. attrImage);
  612. }
  613. }
  614. return bmpDest;
  615. }
  616. public static Font AdjustSize(this Font font, Graphics graphics, string text, int width)
  617. {
  618. Font result = null;
  619. for (int size = (int)font.Size; size > 0; size--)
  620. {
  621. result = new Font(font.Name, size, font.Style);
  622. SizeF adjustedSizeNew = graphics.MeasureString(text, result);
  623. if (width > Convert.ToInt32(adjustedSizeNew.Width))
  624. return result;
  625. }
  626. return result;
  627. }
  628. public static Bitmap WatermarkImage(this Bitmap image, String text, System.Windows.Media.Color color, int maxfontsize = 0)
  629. {
  630. return image.WatermarkImage(text, Color.FromArgb(color.A, color.R, color.G, color.B),maxfontsize);
  631. }
  632. public static Bitmap WatermarkImage(this Bitmap image, String text, Color color, int maxfontsize = 0)
  633. {
  634. int w = image.Width;
  635. int h = image.Height;
  636. Bitmap result = new System.Drawing.Bitmap(w, h);
  637. Graphics graphics = System.Drawing.Graphics.FromImage((System.Drawing.Image)result);
  638. graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  639. graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  640. graphics.Clear(System.Drawing.Color.Transparent);
  641. graphics.DrawImage(image, 0, 0, w, h);
  642. Font drawFont = new System.Drawing.Font("Arial", 96).AdjustSize(graphics,text,(int)(image.Width * 0.9F));
  643. if ((maxfontsize > 0) && (drawFont.Size > maxfontsize))
  644. drawFont = new System.Drawing.Font("Arial", maxfontsize);
  645. SolidBrush drawBrush = new System.Drawing.SolidBrush(color);
  646. StringFormat stringFormat = new StringFormat();
  647. stringFormat.Alignment = StringAlignment.Center;
  648. stringFormat.LineAlignment = StringAlignment.Center;
  649. graphics.DrawString(text, drawFont, drawBrush, new Rectangle(0,0,w,h), stringFormat);
  650. graphics.Dispose();
  651. return result;
  652. }
  653. private static System.Windows.Media.Color AdjustColor(System.Windows.Media.Color color, Action<HSL> action)
  654. {
  655. var hsl = ColorHelper.ColorConverter.RgbToHsl(new RGB(color.R,color.G,color.B));
  656. action(hsl);
  657. var rgb = ColorHelper.ColorConverter.HslToRgb(hsl);
  658. return System.Windows.Media.Color.FromArgb(color.A, rgb.R, rgb.G, rgb.B);
  659. }
  660. private static int AdjustPercentage(int original, int percentage)
  661. {
  662. int percent = Math.Min(100, Math.Max(-100, percentage));
  663. int newvalue = (percent < 0)
  664. ? (byte)((percent * original) / 100)
  665. : (byte)((percent * (100 - original)) / 100);
  666. return original + newvalue;
  667. }
  668. public static System.Windows.Media.Color AdjustHue(this System.Windows.Media.Color color, int degrees) =>
  669. AdjustColor(color, (hsl => hsl.H += degrees));
  670. public static System.Windows.Media.Color AdjustSaturation(this System.Windows.Media.Color color, int percentage) =>
  671. AdjustColor(color, (hsl =>
  672. {
  673. hsl.S = (byte)AdjustPercentage(hsl.S, percentage);
  674. }));
  675. public static System.Windows.Media.Color SetSaturation(this System.Windows.Media.Color color, int percentage) =>
  676. AdjustColor(color, (hsl => hsl.S = (byte)percentage));
  677. public static System.Windows.Media.Color AdjustLightness(this System.Windows.Media.Color color, int percentage) =>
  678. AdjustColor(color, (hsl =>
  679. {
  680. hsl.L = (byte)AdjustPercentage(hsl.L, percentage);
  681. }));
  682. public static System.Windows.Media.Color SetLightness(this System.Windows.Media.Color color, int percentage) =>
  683. AdjustColor(color, (hsl => hsl.L = (byte)percentage));
  684. public static System.Windows.Media.Color SetAlpha(this System.Windows.Media.Color color, byte alpha) =>
  685. System.Windows.Media.Color.FromArgb(alpha, color.R, color.G, color.B);
  686. public static HSL ToHSL(this System.Windows.Media.Color color)
  687. {
  688. return ColorHelper.ColorConverter.RgbToHsl(new RGB(color.R, color.G, color.B));
  689. }
  690. public static System.Windows.Media.Color ToColor(this HSL hsl)
  691. {
  692. var rgb = ColorHelper.ColorConverter.HslToRgb(hsl);
  693. return System.Windows.Media.Color.FromRgb(rgb.R, rgb.G, rgb.B);
  694. }
  695. public static HSL ToHSL(this System.Drawing.Color color)
  696. {
  697. return ColorHelper.ColorConverter.RgbToHsl(new RGB(color.R, color.G, color.B));
  698. }
  699. public static System.Windows.Media.Color GetForegroundColor(this System.Windows.Media.Color c, int threshold = 130)
  700. {
  701. var perceivedbrightness = (int)Math.Sqrt(
  702. c.R * c.R * .299 +
  703. c.G * c.G * .587 +
  704. c.B * c.B * .114);
  705. return perceivedbrightness >= threshold ? Colors.Black : Colors.White;
  706. }
  707. public static uint ToUint(this System.Drawing.Color color) => (uint)((color.A << 24) | (color.R << 16) | (color.G << 8) | (color.B << 0));
  708. public static uint ToUint(this System.Windows.Media.Color color) => (uint)((color.A << 24) | (color.R << 16) | (color.G << 8) | (color.B << 0));
  709. public enum ImageEncoding
  710. {
  711. JPEG,
  712. TIFF,
  713. PNG,
  714. }
  715. public static ImageCodecInfo? GetEncoder(ImageFormat format)
  716. {
  717. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  718. foreach (ImageCodecInfo codec in codecs)
  719. {
  720. if (codec.FormatID == format.Guid)
  721. {
  722. return codec;
  723. }
  724. }
  725. return null;
  726. }
  727. public static byte[] BitmapToPdf(Bitmap bitmap)
  728. {
  729. byte[]? _result = null;
  730. using (var _stream = new MemoryStream())
  731. {
  732. bitmap.Save(_stream, ImageFormat.Png);
  733. _result = BitmapToPdf(_stream);
  734. }
  735. return _result;
  736. }
  737. public static byte[] BitmapToPdf(byte[] bitmap)
  738. {
  739. byte[] _result = null;
  740. using (var _stream = new MemoryStream(bitmap))
  741. _result = BitmapToPdf(_stream);
  742. return _result;
  743. }
  744. public static byte[] BitmapToPdf(Stream stream)
  745. {
  746. byte[]? _result = null;
  747. PdfDocument _doc = new PdfDocument();
  748. PdfPage _page = _doc.Pages.Add();
  749. PdfGraphics _graphics = _page.Graphics;
  750. stream.Position = 0;
  751. PdfBitmap _image = new PdfBitmap(stream);
  752. _graphics.DrawImage(_image, 0, 0);
  753. using (var _out = new MemoryStream())
  754. {
  755. _doc.Save(_out);
  756. _out.Position = 0;
  757. _doc.Close(true);
  758. _result = _out.ToArray();
  759. }
  760. return _result;
  761. }
  762. public static bool IsPdf(byte[] data)
  763. {
  764. var pdfBytes = Encoding.ASCII.GetBytes("%PDF-");
  765. return data.Take(pdfBytes.Length).SequenceEqual(pdfBytes);
  766. }
  767. public static byte[] GetPDFThumbnail(byte[] pdfData, int width, int height)
  768. {
  769. using (var inData = new MemoryStream(pdfData))
  770. {
  771. using (var converter = new PdfToImageConverter(inData))
  772. {
  773. using (var outData = new MemoryStream())
  774. {
  775. converter.Convert(0, new SizeF(width, height), true, false, false)
  776. .CopyTo(outData);
  777. return outData.ToArray();
  778. }
  779. }
  780. }
  781. }
  782. public static byte[] PDFToBitmap(byte[] pdfData, int page)
  783. {
  784. using (var inData = new MemoryStream(pdfData))
  785. {
  786. using (var converter = new PdfToImageConverter(inData))
  787. {
  788. using (var outData = new MemoryStream())
  789. {
  790. converter.Convert(page, false, false).CopyTo(outData);
  791. return outData.ToArray();
  792. }
  793. }
  794. }
  795. }
  796. private static Bitmap[] PDFToBitmapArray(byte[] pdfData)
  797. {
  798. Bitmap[] images = [];
  799. using (var ms = new MemoryStream(pdfData))
  800. {
  801. using (var converter = new PdfToImageConverter(ms))
  802. {
  803. images = converter.Convert(0, converter.PageCount - 1, false, false)
  804. .Select(x => new Bitmap(x))
  805. .ToArray();
  806. }
  807. }
  808. return images;
  809. }
  810. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  811. public static extern bool DeleteObject(IntPtr hObject);
  812. public static List<ImageSource> RenderPDFToImageSources(byte[] pdfData, ImageEncoding encoding = ImageEncoding.JPEG)
  813. {
  814. using var profiler = new Profiler(true);
  815. var rendered = new List<ImageSource>();
  816. var images = PDFToBitmapArray(pdfData);
  817. if (images != null)
  818. foreach (var image in images)
  819. {
  820. var hBitmap = image.GetHbitmap();
  821. try
  822. {
  823. var source = (ImageSource)Imaging.CreateBitmapSourceFromHBitmap(
  824. hBitmap,
  825. IntPtr.Zero,
  826. Int32Rect.Empty,
  827. BitmapSizeOptions.FromEmptyOptions());
  828. source.Freeze();
  829. rendered.Add(source);
  830. }
  831. finally
  832. {
  833. DeleteObject(hBitmap);
  834. }
  835. }
  836. return rendered;
  837. }
  838. public static List<byte[]> RenderPDFToImageBytes(byte[] pdfData, ImageEncoding encoding = ImageEncoding.JPEG)
  839. {
  840. var rendered = new List<byte[]>();
  841. Bitmap[] images = PDFToBitmapArray(pdfData);
  842. ImageCodecInfo encoder;
  843. EncoderParameters? parameters = null;
  844. if (encoding == ImageEncoding.JPEG)
  845. {
  846. encoder = GetEncoder(ImageFormat.Jpeg)!;
  847. parameters = new EncoderParameters(1);
  848. parameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
  849. }
  850. else if (encoding == ImageEncoding.TIFF)
  851. {
  852. encoder = GetEncoder(ImageFormat.Tiff)!;
  853. }
  854. else
  855. {
  856. encoder = GetEncoder(ImageFormat.Png)!;
  857. }
  858. foreach (var image in images)
  859. {
  860. using (var data = new MemoryStream())
  861. {
  862. image.Save(data, encoder, parameters);
  863. rendered.Add(data.ToArray());
  864. }
  865. }
  866. return rendered;
  867. }
  868. public static List<byte[]> RenderTextFileToImages(string textData)
  869. {
  870. var pdfDocument = new PdfDocument();
  871. var page = pdfDocument.Pages.Add();
  872. var font = new PdfStandardFont(PdfFontFamily.Courier, 14);
  873. var textElement = new PdfTextElement(textData, font);
  874. var layoutFormat = new PdfLayoutFormat
  875. {
  876. Layout = PdfLayoutType.Paginate,
  877. Break = PdfLayoutBreakType.FitPage
  878. };
  879. textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
  880. using var docStream = new MemoryStream();
  881. pdfDocument.Save(docStream);
  882. Bitmap[] bmpImages = PDFToBitmapArray(docStream.ToArray());
  883. var jpgEncoder = GetEncoder(ImageFormat.Jpeg)!;
  884. var quality = Encoder.Quality;
  885. var encodeParams = new EncoderParameters(1);
  886. encodeParams.Param[0] = new EncoderParameter(quality, 100L);
  887. var images = new List<byte[]>();
  888. if (bmpImages != null)
  889. foreach (var image in bmpImages)
  890. {
  891. using var data = new MemoryStream();
  892. image.Save(data, jpgEncoder, encodeParams);
  893. images.Add(data.ToArray());
  894. }
  895. return images;
  896. }
  897. public static ContentControl CreatePreviewWindowButtonContent(string caption, Bitmap bitmap)
  898. {
  899. Frame frame = new Frame();
  900. frame.Padding = new Thickness(0);
  901. frame.Margin = new Thickness(10, 10, 10, 5);
  902. Grid grid = new Grid();
  903. grid.Margin = new Thickness(0);
  904. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  905. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
  906. var img = new System.Windows.Controls.Image
  907. {
  908. Source = bitmap.AsBitmapImage(),
  909. Height = 32.0F,
  910. Width = 32.0F,
  911. Margin = new Thickness(10)
  912. };
  913. img.SetValue(Grid.RowProperty, 0);
  914. img.Margin = new Thickness(0);
  915. grid.Children.Add(img);
  916. var txt = new System.Windows.Controls.TextBox();
  917. txt.IsEnabled = false;
  918. txt.Text = caption;
  919. txt.BorderThickness = new Thickness(0);
  920. txt.TextWrapping = TextWrapping.WrapWithOverflow;
  921. txt.MaxWidth = 90;
  922. txt.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
  923. txt.SetValue(Grid.RowProperty, 1);
  924. grid.Children.Add(txt);
  925. frame.Content = grid;
  926. return frame;
  927. }
  928. public static String AsExtension(this ImageFormat format)
  929. {
  930. if (format.Equals(ImageFormat.Bmp) || format.Equals(ImageFormat.MemoryBmp))
  931. return "bmp";
  932. if (format.Equals(ImageFormat.Emf))
  933. return "emf";
  934. if (format.Equals(ImageFormat.Wmf))
  935. return "wmf";
  936. if (format.Equals(ImageFormat.Gif))
  937. return "gif";
  938. if (format.Equals(ImageFormat.Jpeg))
  939. return "jpeg";
  940. if (format.Equals(ImageFormat.Png))
  941. return "png";
  942. if (format.Equals(ImageFormat.Tiff))
  943. return "tiff";
  944. if (format.Equals(ImageFormat.Exif))
  945. return "exif";
  946. if (format.Equals(ImageFormat.Icon))
  947. return "ico";
  948. return "";
  949. }
  950. public static bool IsEqual(this BitmapImage? image1, BitmapImage? image2)
  951. {
  952. if (image1 == null || image2 == null)
  953. return false;
  954. return image1.ToBytes().SequenceEqual(image2.ToBytes());
  955. }
  956. public static byte[] ToBytes(this BitmapImage image)
  957. {
  958. byte[] data = new byte[] { };
  959. if (image != null)
  960. {
  961. try
  962. {
  963. var encoder = new BmpBitmapEncoder();
  964. encoder.Frames.Add(BitmapFrame.Create(image));
  965. using (MemoryStream ms = new MemoryStream())
  966. {
  967. encoder.Save(ms);
  968. data = ms.ToArray();
  969. }
  970. return data;
  971. }
  972. catch (Exception ex)
  973. {
  974. }
  975. }
  976. return data;
  977. }
  978. public static ImageSource? SvgToBitmapImage(Stream stream)
  979. {
  980. using (var image = new MemoryStream())
  981. {
  982. var converter = new StreamSvgConverter(new WpfDrawingSettings());
  983. if (converter.Convert(stream, image))
  984. {
  985. image.Position = 0;
  986. var bitmap = new BitmapImage();
  987. bitmap.BeginInit();
  988. bitmap.StreamSource = image;
  989. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  990. bitmap.EndInit();
  991. bitmap.Freeze();
  992. var drawing = new DrawingImage(converter.Drawing);
  993. return drawing;
  994. }
  995. }
  996. return null;
  997. }
  998. }
  999. }