using System; using System.Drawing; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; namespace KGIS.Plugin.LayerProperty.Utils { public class StyleGalleryItemView { private static IEnvelope Envelope4BatchDraw; private static ITransformation Transformation4BatchDraw; private static IGeometry Geometry4BatchDraw; public static Bitmap StyleGalleryItemToBmp(int iWidth, int iHeight, IStyleGalleryClass mStyleGlyCs, IStyleGalleryItem mStyleGlyItem) { //if (mStyleGlyItem.Item is ISymbol) //{ // return StyleGalleryItemView.SymbolToBitmap(iWidth, iHeight, mStyleGlyItem.Item as ISymbol); //} Bitmap bitmap = new Bitmap(iWidth, iHeight); Graphics graphics = Graphics.FromImage(bitmap); IntPtr hdc = IntPtr.Zero; try { hdc = graphics.GetHdc(); } catch { } try { tagRECT tagRECT = default(tagRECT); tagRECT.right = bitmap.Width; tagRECT.bottom = bitmap.Height; mStyleGlyCs.Preview(mStyleGlyItem.Item, hdc.ToInt32(), ref tagRECT); if (hdc.ToInt32() != 0) { graphics.ReleaseHdc(hdc); graphics.Dispose(); } } catch (Exception ex) { if (hdc.ToInt32() != 0) { graphics.ReleaseHdc(hdc); graphics.Dispose(); } //CoreUtil.AddException(ex); } return bitmap; } public static Bitmap SymbolToBitmap(int iWidth, int iHeight, ISymbol pSymbol) { StyleGalleryItemView.ResetBatchDraw(); Win32Util.InitBitmap(iWidth, iHeight); StyleGalleryItemView.DrawSymbol(Win32Util.InnerDC, iWidth, iHeight, pSymbol, 2); Bitmap bitmap = Win32Util.GetBitmap(iWidth, iHeight); Win32Util.ReleaseDC(); return bitmap; } public static Bitmap StyleGalleryItemToBmp(int iHeight, IStyleGalleryClass mStyleGlyCs, IStyleGalleryItem mStyleGlyItem) { int iWidth = (int)(mStyleGlyCs.PreviewRatio * (double)iHeight); return StyleGalleryItemView.StyleGalleryItemToBmp(iWidth, iHeight, mStyleGlyCs, mStyleGlyItem); } public static Bitmap GetSymbolBitMap(int width, int height, IStyleGalleryClass symbologyStyleClass, ISymbol pSymbol) { try { return StyleGalleryItemView.StyleGalleryItemToBmp(width, height, symbologyStyleClass, new ServerStyleGalleryItemClass { Item = pSymbol }); } catch (Exception ex) { //CoreUtil.AddException(ex); } return null; } public static Bitmap GetSymbolBitMap(int height, IStyleGalleryClass symbologyStyleClass, ISymbol pSymbol) { try { return StyleGalleryItemView.StyleGalleryItemToBmp(height, symbologyStyleClass, new ServerStyleGalleryItemClass { Item = pSymbol }); } catch (Exception ex) { //CoreUtil.AddException(ex); } return null; } public static Image CreatePictureFromSymbol(ISymbol pSymbol, double dblWidth, double dblHeight, double dblGap) { Bitmap bitmap = new Bitmap((int)dblWidth, (int)dblHeight); Graphics graphics = Graphics.FromImage(bitmap); IntPtr hdc = (IntPtr)0; hdc = graphics.GetHdc(); StyleGalleryItemView.DrawToDC(hdc.ToInt32(), dblWidth, dblHeight, dblGap, pSymbol, false); graphics.ReleaseHdc(hdc); graphics.Dispose(); return bitmap; } public static Image CreatePictureFromSymbol(ISymbol pSymbol, double dblWidth, double dblHeight, double dblGap, bool blnLine) { Bitmap bitmap = new Bitmap((int)dblWidth, (int)dblHeight); Graphics graphics = Graphics.FromImage(bitmap); IntPtr hdc = (IntPtr)0; hdc = graphics.GetHdc(); StyleGalleryItemView.DrawToDC(hdc.ToInt32(), dblWidth, dblHeight, dblGap, pSymbol, blnLine); graphics.ReleaseHdc(hdc); graphics.Dispose(); return bitmap; } private static void DrawToDC(int hDC, double dblWidth, double dblHeight, double dblGap, ISymbol pSymbol, bool blnLine) { IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(dblGap, dblGap, dblWidth - dblGap, dblHeight - dblGap); ITransformation transformation = StyleGalleryItemView.CreateTransFromDC(hDC, dblWidth, dblHeight); IGeometry geometry = StyleGalleryItemView.CreateSymShape(pSymbol, envelope, blnLine); pSymbol.SetupDC(hDC, transformation); pSymbol.Draw(geometry); pSymbol.ResetDC(); } private static ITransformation CreateTransFromDC(int hDC, double dblWidth, double dblHeight) { IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(0.0, 0.0, dblWidth, dblHeight); tagRECT tagRECT = default(tagRECT); tagRECT.left = 0; tagRECT.top = 0; tagRECT.right = (int)dblWidth; tagRECT.bottom = (int)dblHeight; return new DisplayTransformationClass { VisibleBounds = envelope, Bounds = envelope, //DeviceFrame = ref tagRECT, Resolution = 96.0 }; } private static IGeometry CreateSymShape(ISymbol pSymbol, IEnvelope pEnvelope, bool blnLine) { if (pSymbol is IMarkerSymbol) { IArea area = pEnvelope as IArea; return area.Centroid; } if (!(pSymbol is ILineSymbol) && !(pSymbol is ITextSymbol)) { return pEnvelope; } if (blnLine) { IPointCollection pointCollection = new PolylineClass(); IPoint point = new PointClass(); point.PutCoords(pEnvelope.XMin, pEnvelope.YMax); object missing = Type.Missing; pointCollection.AddPoint(point, ref missing, ref missing); point = new PointClass(); point.PutCoords(pEnvelope.XMin + pEnvelope.Width / 3.0, pEnvelope.YMin); missing = Type.Missing; pointCollection.AddPoint(point, ref missing, ref missing); point = new PointClass(); point.PutCoords(pEnvelope.XMax - pEnvelope.Width / 3.0, pEnvelope.YMax); missing = Type.Missing; pointCollection.AddPoint(point, ref missing, ref missing); point = new PointClass(); point.PutCoords(pEnvelope.XMax, pEnvelope.YMin); missing = Type.Missing; pointCollection.AddPoint(point, ref missing, ref missing); return pointCollection as IPolyline; } IPolyline polyline = new PolylineClass(); IPoint point2 = new PointClass(); point2.PutCoords(pEnvelope.XMin, pEnvelope.YMin + pEnvelope.Height / 2.0); IPoint point3 = new PointClass(); point3.PutCoords(pEnvelope.XMax, pEnvelope.YMin + pEnvelope.Height / 2.0); polyline.FromPoint = point2; polyline.ToPoint = point3; return polyline; } public static void ResetBatchDraw() { StyleGalleryItemView.Envelope4BatchDraw = null; StyleGalleryItemView.Transformation4BatchDraw = null; StyleGalleryItemView.Geometry4BatchDraw = null; } public static void DrawSymbol(int hDC, int w, int h, ISymbol pSymbol, int gap) { Win32Util.ClearDC(hDC, Color.Transparent, 0, 0, w, h); if (StyleGalleryItemView.Envelope4BatchDraw == null) { StyleGalleryItemView.Envelope4BatchDraw = new EnvelopeClass(); } StyleGalleryItemView.Envelope4BatchDraw.PutCoords((double)gap, (double)gap, (double)(w - gap), (double)(h - gap)); if (StyleGalleryItemView.Transformation4BatchDraw == null) { StyleGalleryItemView.Transformation4BatchDraw = StyleGalleryItemView.CreateTransFromDC(hDC, (double)w, (double)h); } if (StyleGalleryItemView.Geometry4BatchDraw == null) { StyleGalleryItemView.Geometry4BatchDraw = StyleGalleryItemView.CreateSymShape(pSymbol, StyleGalleryItemView.Envelope4BatchDraw); } try { if (pSymbol is ITextSymbol && pSymbol is ITextSymbol) { ((ITextSymbol)pSymbol).Text = "AaBbYyZz"; } pSymbol.SetupDC(hDC, StyleGalleryItemView.Transformation4BatchDraw); pSymbol.Draw(StyleGalleryItemView.Geometry4BatchDraw); } catch (Exception value) { Console.Write(value); } finally { pSymbol.ResetDC(); } } private static IGeometry CreateSymShape(ISymbol pSymbol, IEnvelope pEnvelope) { if (pSymbol is IMarkerSymbol) { IArea area = (IArea)pEnvelope; return area.Centroid; } if (pSymbol is ILineSymbol || pSymbol is ITextSymbol) { IPolyline polyline = (IPolyline)new PolylineClass(); IPoint point = new PointClass(); point.X = (pEnvelope.LowerLeft.X + pEnvelope.UpperLeft.X) / 2.0; point.Y = (pEnvelope.LowerLeft.Y + pEnvelope.UpperLeft.Y) / 2.0; polyline.FromPoint = point; point.X = (pEnvelope.LowerRight.X + pEnvelope.UpperRight.X) / 2.0; point.Y = (pEnvelope.LowerRight.Y + pEnvelope.UpperRight.Y) / 2.0; polyline.ToPoint = point; return polyline; } return pEnvelope; } } }