Convert the symbol or object in symbologycontrol to an image (VB. NET source code)

Source: Internet
Author: User

When we use symbologycontrol to set the style of a layer symbol or to set the style of the finger or scale,

We need to preview these selected styles and display them as images in picturebox. In this case, we need to get the image of the selected object,

The following two methods are provided:

I. It is implemented by using the previewitem method provided by AE. This method is universally applicable and can be applied to all objects supported by symbolgycontrol, such as (Northern finger, scale, symbol, etc)

2. It is implemented using the draw function of symbol. Currently, this method only supports symbol and can be expanded.

 

Method 1:

''' <Summary>
''' Get the image through symbolobject
''' </Summary>
''' <Param name = "pstylegallery"> selected items in the passed symbologycontrol </param>
''' <Param name = "pstyleclass"> current type </param>
''' <Param name = "pwidth"> image width </param>
''' <Param name = "pheight"> image height </param>
''' <Returns> the obtained image </returns>
''' <Remarks> </remarks>
Shared function getimagefromsymbolobject (byval pstylegallery as istylegalleryitem, byval pstyleclass as esrisymbologystyleclass, byval pwidth as double, byval pheight as double) as image
Dim psymbology as isymbologycontrol = new symbologycontrol
Psymbology. styleclass = pstyleclass
Psymbology. getstyleclass (psymbology. styleclass). additem (pstylegallery, 0)
Dim picture as stdole. ipicturedisp = psymbology. getstyleclass (psymbology. styleclass). previewitem (pstylegallery, pwidth, pheight)
Dim image as system. Drawing. Image = system. Drawing. image. fromhbitmap (new system. intptr (picture. Handle ))
Return Image
End Function
''' <Summary>
''' Get the image through symbolobject
''' </Summary>
''' <Param name = "psymbology"> passed in symbologycontrol </param>
''' <Param name = "pstylegallery"> selected items in the passed symbologycontrol </param>
''' <Param name = "pwidth"> image width </param>
''' <Param name = "pheight"> image height </param>
''' <Returns> the obtained image </returns>
''' <Remarks> </remarks>
Shared function getimagefromsymbolobject (byval psymbology as isymbologycontrol, byval pstylegallery as istylegalleryitem, byval pwidth as double, byval pheight as double) as image
Dim picture as stdole. ipicturedisp = psymbology. getstyleclass (psymbology. styleclass). previewitem (pstylegallery, pwidth, pheight)
Dim image as system. Drawing. Image = system. Drawing. image. fromhbitmap (new system. intptr (picture. Handle ))
Return Image
End Function
''' <Summary>
''' Get the image through symbolobject
''' </Summary>
''' <Param name = "pstyleclass"> current type </param>
''' <Param name = "psymbolobject"> input objects, such as textsymbol and northarrow </param>
''' <Param name = "pwidth"> image width </param>
''' <Param name = "pheight"> image height </param>
''' <Returns> the obtained image </returns>
''' <Remarks> </remarks>
Shared function getimagefromsymbolobject (byval pstyleclass as esrisymbologystyleclass, byval psymbolobject as object, byval pwidth as double, byval pheight as double) as image
Dim psymbology as isymbologycontrol = new symbologycontrol
Dim pstylegallery as istylegalleryitem = new serverstylegalleryitem
Pstylegallery. Name = "current symbol"
Pstylegallery. Item = psymbolobject
Psymbology. getstyleclass (pstyleclass). additem (pstylegallery, 0)
Dim picture as stdole. ipicturedisp = psymbology. getstyleclass (pstyleclass). previewitem (pstylegallery, pwidth, pheight)
Dim image as system. Drawing. Image = system. Drawing. image. fromhbitmap (new system. intptr (picture. Handle ))
Return Image
End Function
''' <Summary>
''' Get the image through symbolobject
''' </Summary>
''' <Param name = "psymbolobject"> input objects, such as textsymbol and northarrow </param>
''' <Param name = "pwidth"> image width </param>
''' <Param name = "pheight"> image height </param>
''' <Returns> the obtained image </returns>
''' <Remarks> </remarks>
Shared function getimagefromsymbolobject (byval psymbolobject as object, byval pwidth as double, byval pheight as double) as image
Dim psymbology as isymbologycontrol = new symbologycontrol
Dim pstylegallery as istylegalleryitem = new serverstylegalleryitem
Pstylegallery. Name = "current symbol"
Pstylegallery. Item = psymbolobject
Dim pstyleclass as esrisymbologystyleclass = getstyleclass (psymbolobject)
Psymbology. getstyleclass (pstyleclass). additem (pstylegallery, 0)
Dim picture as stdole. ipicturedisp = psymbology. getstyleclass (pstyleclass). previewitem (pstylegallery, pwidth, pheight)
Dim image as system. Drawing. Image = system. Drawing. image. fromhbitmap (new system. intptr (picture. Handle ))
Return Image
End Function

''' <Summary>
''' Get the corresponding styleclass
''' </Summary>
''' <Param name = "psymbolobject"> current object </param>
''' <Returns> </returns>
''' <Remarks> </remarks>
Shared function getstyleclass (byval psymbolobject as object) as esrisymbologystyleclass
If typeof psymbolobject is itextsymbol then
Return esrisymbologystyleclass. esristyleclasstextsymbols
Elseif typeof psymbolobject is imarkersymbol then
Return esrisymbologystyleclass. esristyleclassmarkersymbols
Elseif typeof psymbolobject is ilinesymbol then
Return esrisymbologystyleclass. esristyleclasslinesymbols
Elseif typeof psymbolobject is ifillsymbol then
Return esrisymbologystyleclass. esristyleclassfillsymbols
Elseif typeof psymbolobject is iscaletext then
Return esrisymbologystyleclass. esristyleclassscaletexts
Elseif typeof psymbolobject is iscalebar then
Return esrisymbologystyleclass. esristyleclassscalebars
Elseif typeof psymbolobject is inortharrow then
Return esrisymbologystyleclass. esristyleclassnortharrows
Elseif typeof psymbolobject is ibackground then
Return esrisymbologystyleclass. esristyleclassbackgrounds
Elseif typeof psymbolobject is iborder then
Return esrisymbologystyleclass. esristyleclassborders
Elseif typeof psymbolobject is ishadow then
Return esrisymbologystyleclass. esristyleclasslabels
Elseif typeof psymbolobject is ilegend then
Return esrisymbologystyleclass. esristyleclasslegenditems
End if
End Function

 

Method 2:

 ''' <Summary>
''' Converts a symbol to a bitmap.
''' </Summary>
''' <Param name = "psymbol"> symbol </param>
''' <Param name = "iwidth"> image width </param>
''' <Param name = "iheight"> image height </param>
''' <Returns> Returns bitmap </returns>
''' <Remarks> </remarks>
Shared function symboltobitmp (byval psymbol as isymbol, byval iwidth as integer, byval iheight as integer) as system. Drawing. Bitmap
''Creates an image based on the height and width
Dim BMP as drawing. Bitmap = new drawing. Bitmap (iwidth, iheight)
Dim gimage as drawing. Graphics = drawing. Graphics. fromimage (BMP)
Gimage. Clear (drawing. color. White)

dim DPI as double = gimage. dpix
dim penvelope as ienvelope = new envelopeclass ()
penvelope. putcoords (0, 0, BMP. width, BMP. height)
dim devicerect as new tagrect
devicerect. left = 0
devicerect. right = BMP. width
devicerect. top = 0
devicerect. bottom = BMP. height
dim pdisplaytransformation as idisplaytransformation = new displaytransformationclass
pdisplaytransformation. visiblebounds = penvelope
pdisplaytransformation. bounds = penvelope
pdisplaytransformation. deviceframe = devicerect
pdisplaytransformation. resolution = DPI

Dim pgeo as igeometry = createsymshape (psymbol, penvelope)
Dim HDC as system. intptr = new intptr ()
HDC = gimage. gethdc ()
''Draws the shape of the symbol into the image.
Psymbol. setupdc (HDC, pdisplaytransformation)
Psymbol. Draw (pgeo)
Psymbol. resetdc ()
Gimage. releasehdc (HDC)
Gimage. Dispose ()
Return BMP
End Function

Shared function createsymshape (byval psymbol as isymbol, byval penvelope as ienvelope) as igeometry

Dim pgeo as igeometry = nothing
''Returns the corresponding geometric space entity (point, line, and surface) based on the passed symbol and the outsourced rectangular area)
''Indicates whether it is a dot.
If typeof psymbol is imarkersymbol then
Dim pmarkersym as imarkersymbol = psymbol
If pmarkersym isnot nothing then
''Indicates the dot, and the ienvelope center point is returned.
Dim parea as iarea = penvelope
PGO = parea. Centroid
End if
Elseif typeof psymbol is ilinesymbol then
''Indicates whether it is a line.
Dim ilinesym as ilinesymbol = psymbol
If ilinesym isnot nothing then
''Returns the diagonal line of 45 degrees.
Dim ipline as ipolympus line
Ipline = new polylineclass ()
Ipline. frompoint = penvelope. lowerleft
Ipline. topoint = penvelope. upperright
PGO = ipline
End if
Elseif typeof psymbol is ifillsymbol then
''Directly returns an ienvelope rectangular area.
PGO = penvelope
End if
Return pgeo
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.