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