private Bitmap DrawToPictureBox(ISymbol pSym, PictureBox pBox) { IGeometry pGeometry = null; System.Drawing.Graphics pGraphics = null; pGraphics = System.Drawing.Graphics.FromHwnd(pBox.Handle); pGraphics.FillRectangle(System.Drawing.Brushes.White, pBox.ClientRectangle); Bitmap image = new Bitmap(pBox.Width, pBox.Height, pGraphics); IEnvelope pEnvelope = new EnvelopeClass(); if (pSym is IMarkerSymbol) { IPoint pPoint = new PointClass(); //the geometry of a MarkerSymbol pPoint.PutCoords(pBox.Width / 2, pBox.Height / 2); //center in middle of pBox pGeometry = pPoint; } if (pSym is ILineSymbol) { pEnvelope.PutCoords(0, pBox.Height / 2, pBox.Width, pBox.Height / 2); IPolyline pPolyline = new PolylineClass(); pPolyline.FromPoint = pEnvelope.LowerLeft; pPolyline.ToPoint = pEnvelope.UpperRight; pGeometry = pPolyline; } if (pSym is IFillSymbol) { pEnvelope.PutCoords(1, 1, pBox.Width - 1, pBox.Height - 1); pGeometry = pEnvelope; if (pSym is IMultiLayerFillSymbol) { IMultiLayerFillSymbol pMultiLayerFillSymbol = pSym as IMultiLayerFillSymbol; //For mLayers As Integer = 0 To pMultiLayerFillSymbol.LayerCount - 1 for (int i = 0; i < pMultiLayerFillSymbol.LayerCount; i++) { if (pMultiLayerFillSymbol.get_Layer(i) is IPictureFillSymbol) { IPictureFillSymbol pPictureFillSymbol = pMultiLayerFillSymbol.get_Layer(0) as IPictureFillSymbol; image = Bitmap.FromHbitmap(new IntPtr(pPictureFillSymbol.Picture.Handle)); //m.MakeTransparent(System.Drawing.ColorTranslator.FromOle(pPictureFillSymbol.Color.RGB)) return(image); } else if (pMultiLayerFillSymbol.get_Layer(i) is ISimpleFillSymbol) { pEnvelope.PutCoords(1, 1, pBox.Width - 1, pBox.Height - 1); pGeometry = pEnvelope; } else if (pMultiLayerFillSymbol.get_Layer(i) is IGradientFillSymbol) { IGradientFillSymbol pGradientFillSymbol = pMultiLayerFillSymbol.get_Layer(0) as IGradientFillSymbol; IAlgorithmicColorRamp pRamp = pGradientFillSymbol.ColorRamp as IAlgorithmicColorRamp; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), new System.Drawing.Size(pBox.Width, pBox.Height)); LinearGradientBrush lgBrush = new LinearGradientBrush(rect, System.Drawing.ColorTranslator.FromOle(pRamp.FromColor.RGB), System.Drawing.ColorTranslator.FromOle(pRamp.ToColor.RGB), 45); pGraphics.FillRectangle(lgBrush, rect); rect.Width = rect.Width - 1; rect.Height = rect.Height - 1; pGraphics.DrawRectangle(new Pen(ColorTranslator.FromOle(pGradientFillSymbol.Outline.Color.RGB), (float)pGradientFillSymbol.Outline.Width), rect); image = new Bitmap(pBox.Width, pBox.Height, pGraphics); } } } else if (pSym is IPictureFillSymbol) { IPictureFillSymbol pPictureFillSymbol = pSym as IPictureFillSymbol; image = Bitmap.FromHbitmap(new IntPtr(pPictureFillSymbol.Picture.Handle)); //m.MakeTransparent(System.Drawing.ColorTranslator.FromOle(pPictureFillSymbol.Color.RGB)) return(image); } } IntPtr hDC = GetDC(pBox.Handle); pSym.SetupDC(hDC.ToInt32(), null); pSym.ROP2 = esriRasterOpCode.esriROPCopyPen; if (pGeometry != null) { pSym.Draw(pGeometry); } pSym.ResetDC(); Graphics g2 = Graphics.FromImage(image); //获得屏幕的句柄 IntPtr dc3 = pGraphics.GetHdc(); //获得位图的句柄 IntPtr dc2 = g2.GetHdc(); BitBlt(dc2, 0, 0, pBox.Width, pBox.Height, dc3, 0, 0, SRCCOPY); pGraphics.ReleaseHdc(dc3); //释放屏幕句柄 g2.ReleaseHdc(dc2); //释放位图句柄 //image.Save("c:\\MyJpeg.Icon", ImageFormat.Bmp); return(image); }
private void DrawToPictureBox(ISymbol pSym, PictureBox pBox) { IGeometry pGeometry = null; IntPtr hDC; IEnvelope pEnvelope; pEnvelope = new EnvelopeClass(); // Reset the PictureBox pBox.CreateGraphics().Clear(pBox.BackColor); try { if (pSym is IMarkerSymbol) { pEnvelope.PutCoords(pBox.Width / 2, pBox.Height / 2, pBox.Width / 2, pBox.Height / 2); IArea pArea = pEnvelope as IArea; pGeometry = pArea.Centroid; } else if (pSym is ILineSymbol) { pEnvelope.PutCoords(0, pBox.Height / 2, pBox.Width, pBox.Height / 2); IPolyline pPolyline = new PolylineClass(); pPolyline.FromPoint = pEnvelope.LowerLeft; pPolyline.ToPoint = pEnvelope.UpperRight; pGeometry = pPolyline; } else if (pSym is IFillSymbol) { if (pSym is SimpleFillSymbol) { pEnvelope.PutCoords(5, 5, pBox.Width - 5, pBox.Height - 5); pGeometry = pEnvelope; } else if (pSym is MultiLayerFillSymbol) { IMultiLayerFillSymbol pMultiLayerFillSymbol = pSym as IMultiLayerFillSymbol; //For mLayers As Integer = 0 To pMultiLayerFillSymbol.LayerCount - 1 if (pMultiLayerFillSymbol.get_Layer(0) is PictureFillSymbol) { IPictureFillSymbol pPictureFillSymbol = pMultiLayerFillSymbol.get_Layer(0) as IPictureFillSymbol; Bitmap m = Bitmap.FromHbitmap(new IntPtr(pPictureFillSymbol.Picture.Handle)); //m.MakeTransparent(System.Drawing.ColorTranslator.FromOle(pPictureFillSymbol.Color.RGB)) pBox.Image = m; return; } else if (pMultiLayerFillSymbol.get_Layer(0) is SimpleFillSymbol) { pEnvelope.PutCoords(5, 5, pBox.Width - 5, pBox.Height - 5); pGeometry = pEnvelope; } else if (pMultiLayerFillSymbol.get_Layer(0) is GradientFillSymbol) { IGradientFillSymbol pGradientFillSymbol = pMultiLayerFillSymbol.get_Layer(0) as IGradientFillSymbol; IAlgorithmicColorRamp pRamp = pGradientFillSymbol.ColorRamp as IAlgorithmicColorRamp; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), new System.Drawing.Size(pBox.Width, pBox.Height)); LinearGradientBrush lgBrush = new LinearGradientBrush(rect, System.Drawing.ColorTranslator.FromOle(pRamp.FromColor.RGB), System.Drawing.ColorTranslator.FromOle(pRamp.ToColor.RGB), 45); Graphics g = pBox.CreateGraphics(); g.FillRectangle(lgBrush, rect); rect.Width = rect.Width - 1; rect.Height = rect.Height - 1; g.DrawRectangle(new Pen(ColorTranslator.FromOle(pGradientFillSymbol.Outline.Color.RGB), (float)pGradientFillSymbol.Outline.Width), rect); } } else if (pSym is PictureFillSymbol) { IPictureFillSymbol pPictureFillSymbol = pSym as IPictureFillSymbol; Bitmap m = Bitmap.FromHbitmap(new IntPtr(pPictureFillSymbol.Picture.Handle)); //m.MakeTransparent(System.Drawing.ColorTranslator.FromOle(pPictureFillSymbol.Color.RGB)) pBox.Image = m; return; } } hDC = GetDC(pBox.Handle); pSym.SetupDC(hDC.ToInt32(), null); pSym.ROP2 = esriRasterOpCode.esriROPCopyPen; if (pGeometry != null) { pSym.Draw(pGeometry); } pSym.ResetDC(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }