1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Drawing.Drawing2D;
|
---|
4 | using System.Runtime.InteropServices;
|
---|
5 |
|
---|
6 | namespace Netron.Diagramming.Core {
|
---|
7 | public class ImageExporter {
|
---|
8 | public static Bitmap FromBundle(IBundle bundle, Graphics g) {
|
---|
9 | // The entities will be drawn to our graphics surface using
|
---|
10 | // their locations on the canvas. We want them to be drawn
|
---|
11 | // relative to the 0,0 (upper, left corner) of our image.
|
---|
12 | // Therefore, calculate the offset required to position them
|
---|
13 | // (normalize) in our image and apply that translation to
|
---|
14 | // g2, the GDI+ drawing surface used to paint the entities to
|
---|
15 | // the image. Notice we're actually calculating the offset
|
---|
16 | // relative to location 5,5. We adjusting the image height
|
---|
17 | // and widht by 10 to ensure we catch everything, so by using
|
---|
18 | // location 5,5 here we kept things centered.
|
---|
19 | Point offset = new Point(
|
---|
20 | 5 - bundle.Rectangle.X,
|
---|
21 | 5 - bundle.Rectangle.Y);
|
---|
22 | Matrix matrix = new Matrix();
|
---|
23 | matrix.Translate(offset.X, offset.Y);
|
---|
24 |
|
---|
25 | Rectangle bundleArea = bundle.Rectangle;
|
---|
26 | // bundleArea.Inflate(5, 5);
|
---|
27 |
|
---|
28 | Bitmap image = new Bitmap(
|
---|
29 | bundleArea.Width + 10,
|
---|
30 | bundleArea.Height + 10,
|
---|
31 | g);
|
---|
32 | Graphics g2 = Graphics.FromImage(image);
|
---|
33 | g2.Transform = matrix;
|
---|
34 |
|
---|
35 |
|
---|
36 | // The background color is a weird blue, so I'm filling the
|
---|
37 | // entire area first with a white color to get around this.
|
---|
38 | g2.Clear(Color.White);
|
---|
39 | g2.SmoothingMode = SmoothingMode.HighQuality;
|
---|
40 | g2.CompositingQuality = CompositingQuality.HighQuality;
|
---|
41 | g2.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
---|
42 |
|
---|
43 | // Deselect and set Hovered to 'false' for all entities so
|
---|
44 | // they're drawn to the image in their *normal* state.
|
---|
45 | bundle.DeSelectAll();
|
---|
46 | bundle.SetHovered(false);
|
---|
47 | bundle.Paint(g2);
|
---|
48 |
|
---|
49 | g.Flush();
|
---|
50 | g2.Flush();
|
---|
51 | g.Dispose();
|
---|
52 | g2.Dispose();
|
---|
53 |
|
---|
54 | //System.IntPtr dc1 = g.GetHdc();
|
---|
55 |
|
---|
56 | //System.IntPtr dc2 = g2.GetHdc();
|
---|
57 |
|
---|
58 | //BitBlt(dc2, 0, 0, width, height, dc1, 0, 0, 0x00CC0020);
|
---|
59 |
|
---|
60 | //g.ReleaseHdc(dc1);
|
---|
61 |
|
---|
62 | //g2.ReleaseHdc(dc2);
|
---|
63 |
|
---|
64 | //g.Dispose();
|
---|
65 |
|
---|
66 | //g2.Dispose();
|
---|
67 | return image;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // ------------------------------------------------------------------
|
---|
71 | /// <summary>
|
---|
72 | /// GDI32 imported function not available in the framework,
|
---|
73 | /// used here to create an image.
|
---|
74 | /// </summary>
|
---|
75 | /// <param name="hdcDest"></param>
|
---|
76 | /// <param name="nXDest"></param>
|
---|
77 | /// <param name="nYDest"></param>
|
---|
78 | /// <param name="nWidth"></param>
|
---|
79 | /// <param name="nHeight"></param>
|
---|
80 | /// <param name="hdcSrc"></param>
|
---|
81 | /// <param name="nXSrc"></param>
|
---|
82 | /// <param name="nYSrc"></param>
|
---|
83 | /// <param name="dwRop"></param>
|
---|
84 | /// <returns></returns>
|
---|
85 | // ------------------------------------------------------------------
|
---|
86 | [DllImport("gdi32.dll")]
|
---|
87 | private static extern bool BitBlt(
|
---|
88 |
|
---|
89 | IntPtr hdcDest, // handle to destination DC
|
---|
90 |
|
---|
91 | int nXDest, // x-coord of destination upper-left corner
|
---|
92 |
|
---|
93 | int nYDest, // y-coord of destination upper-left corner
|
---|
94 |
|
---|
95 | int nWidth, // width of destination rectangle
|
---|
96 |
|
---|
97 | int nHeight, // height of destination rectangle
|
---|
98 |
|
---|
99 | IntPtr hdcSrc, // handle to source DC
|
---|
100 |
|
---|
101 | int nXSrc, // x-coordinate of source upper-left corner
|
---|
102 |
|
---|
103 | int nYSrc, // y-coordinate of source upper-left corner
|
---|
104 |
|
---|
105 | System.Int32 dwRop // raster operation code
|
---|
106 |
|
---|
107 | );
|
---|
108 | }
|
---|
109 | }
|
---|