HatchStyles.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This sample demonstrates the different hatch brush styles.
  19. // Many of those styles implement MS Excel pattern styles.
  20. public class HatchStyles
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. // HatchStyle
  25. var hatchStyles = Enum.GetValues(typeof(HatchStyle));
  26. int COLS = 4;
  27. var w = pixelSize.Width / COLS;
  28. var h = pixelSize.Height / ((hatchStyles.Length + COLS -1) / COLS);
  29.  
  30. var tf = new TextFormat
  31. {
  32. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  33. FontSize = 12,
  34. };
  35.  
  36. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  37. using (var g = bmp.CreateGraphics(Color.White))
  38. {
  39. int row = 0, col = 0;
  40. foreach (HatchStyle hs in hatchStyles)
  41. {
  42. // Hatch:
  43. var rc = new RectangleF(col * w, row * h, w, h);
  44. var b = new HatchBrush(hs)
  45. {
  46. ForeColor = Color.Black,
  47. BackColor = Color.White
  48. };
  49. g.FillRectangle(rc, b);
  50. // Caption:
  51. var cap = hs.ToString();
  52. var scap = g.MeasureString(cap, tf);
  53. var rcap = new RectangleF(
  54. rc.X + (rc.Width - scap.Width) / 2,
  55. rc.Y + (rc.Height - scap.Height) / 2,
  56. scap.Width, scap.Height);
  57. rcap.Inflate(3, 2);
  58. g.FillRectangle(rcap, Color.White);
  59. g.DrawString(cap, tf, rcap, TextAlignment.Center, ParagraphAlignment.Center);
  60. // Move on:
  61. if (++col >= COLS)
  62. {
  63. col = 0;
  64. ++row;
  65. }
  66. }
  67. }
  68. return bmp;
  69. }
  70. }
  71. }
  72.