Advertisement
Guest User

DevExpress SVG Extensions

a guest
Nov 13th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.23 KB | None | 0 0
  1.     Public Module CoreUiSvgExtensions
  2.         <Extension>
  3.         Public Function ToImage(svgImg As DevExpress.Utils.Svg.SvgImage, squareSize As Integer) As Image
  4.             Return svgImg.ToImage(New Size(squareSize, squareSize))
  5.         End Function
  6.  
  7.         <Extension>
  8.         Public Function ToImage(svgImg As SvgImage, size As Size) As Image
  9.             Return svgImg.ToSvgBitmap.Render(size, DevExpress.Utils.Svg.SvgPaletteHelper.GetSvgPalette(DevExpress.LookAndFeel.UserLookAndFeel.Default, DevExpress.Utils.Drawing.ObjectState.Normal))
  10.         End Function
  11.  
  12.         <Extension>
  13.         Public Function ToSvgBitmap(svgImg As SvgImage) As SvgBitmap
  14.             Return New SvgBitmap(svgImg)
  15.         End Function
  16.  
  17.         <Extension>
  18.         Public Function ToIcon(svgImg As SvgImage, squareSize As Integer) As Icon
  19.             Return svgImg.ToImage(squareSize).ToIcon(squareSize)
  20.         End Function
  21.  
  22.         <Extension()>
  23.         Public Function ToIcon(img As Image) As Icon
  24.             Return img.ToIcon(img.Width)
  25.         End Function
  26.  
  27.         <Extension>
  28.         Public Function ToIcon(img As Image, squareSize As Integer) As Icon
  29.             Using ms = New System.IO.MemoryStream()
  30.                 Using bw = New System.IO.BinaryWriter(ms)
  31.                     bw.Write(CShort(0)) 'bytes 0-1 reserved header
  32.                     bw.Write(CShort(1)) 'bytes 2-3 image type, 1=icon, 2=cursor
  33.                     bw.Write(CShort(1)) 'bytes 4-5 image count
  34.                     'image entry 1
  35.                     Dim w = img.Width
  36.                     If w >= 256 Then w = 0 ' 0 means 256 size (as byte can't represent 256)
  37.                     bw.Write(CByte(w)) ' byte 0, width
  38.                     Dim h = img.Height
  39.                     If h >= 256 Then h = 0 ' 0 means 256 size (as byte can't represent 256)
  40.                     bw.Write(CByte(h)) ' byte 1, height
  41.                     bw.Write(CByte(0)) ' byte 2, plane of colors, 0 no color palette
  42.                     bw.Write(CByte(0)) ' byte 3, reserved, should be o
  43.                     bw.Write(CShort(0)) ' byte 4-5, color plane, 0 auto determined
  44.                     bw.Write(CShort(0)) ' byte 6-7, bits per pixel 0 auto determined
  45.                     Dim posOfSizeData = ms.Position
  46.                     bw.Write(CInt(0)) ' byte 8-11, size of image file, temporary value, will update once written data from image
  47.                     Dim posOfImageData = CInt(ms.Position) + 4 ' number of bytes from start of record for image data, current position plus the offset about to be written
  48.                     bw.Write(posOfImageData) ' byte 12-15, offset of image data
  49.                     img.Save(ms, System.Drawing.Imaging.ImageFormat.Png) ' write image data to stream
  50.  
  51.                     Dim imageSize = CInt(ms.Position) - posOfImageData ' current position subtract start of image data is size
  52.  
  53.                     ms.Seek(posOfSizeData, System.IO.SeekOrigin.Begin) ' seek back to pos of size data
  54.                     bw.Write(imageSize) ' write actual image size
  55.                     ms.Seek(0, System.IO.SeekOrigin.Begin) 'seek back to start
  56.  
  57.                     Return New Icon(ms) 'create new icon
  58.                 End Using
  59.             End Using
  60.         End Function
  61.     End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement