Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Module CoreUiSvgExtensions
- <Extension>
- Public Function ToImage(svgImg As DevExpress.Utils.Svg.SvgImage, squareSize As Integer) As Image
- Return svgImg.ToImage(New Size(squareSize, squareSize))
- End Function
- <Extension>
- Public Function ToImage(svgImg As SvgImage, size As Size) As Image
- Return svgImg.ToSvgBitmap.Render(size, DevExpress.Utils.Svg.SvgPaletteHelper.GetSvgPalette(DevExpress.LookAndFeel.UserLookAndFeel.Default, DevExpress.Utils.Drawing.ObjectState.Normal))
- End Function
- <Extension>
- Public Function ToSvgBitmap(svgImg As SvgImage) As SvgBitmap
- Return New SvgBitmap(svgImg)
- End Function
- <Extension>
- Public Function ToIcon(svgImg As SvgImage, squareSize As Integer) As Icon
- Return svgImg.ToImage(squareSize).ToIcon(squareSize)
- End Function
- <Extension()>
- Public Function ToIcon(img As Image) As Icon
- Return img.ToIcon(img.Width)
- End Function
- <Extension>
- Public Function ToIcon(img As Image, squareSize As Integer) As Icon
- Using ms = New System.IO.MemoryStream()
- Using bw = New System.IO.BinaryWriter(ms)
- bw.Write(CShort(0)) 'bytes 0-1 reserved header
- bw.Write(CShort(1)) 'bytes 2-3 image type, 1=icon, 2=cursor
- bw.Write(CShort(1)) 'bytes 4-5 image count
- 'image entry 1
- Dim w = img.Width
- If w >= 256 Then w = 0 ' 0 means 256 size (as byte can't represent 256)
- bw.Write(CByte(w)) ' byte 0, width
- Dim h = img.Height
- If h >= 256 Then h = 0 ' 0 means 256 size (as byte can't represent 256)
- bw.Write(CByte(h)) ' byte 1, height
- bw.Write(CByte(0)) ' byte 2, plane of colors, 0 no color palette
- bw.Write(CByte(0)) ' byte 3, reserved, should be o
- bw.Write(CShort(0)) ' byte 4-5, color plane, 0 auto determined
- bw.Write(CShort(0)) ' byte 6-7, bits per pixel 0 auto determined
- Dim posOfSizeData = ms.Position
- bw.Write(CInt(0)) ' byte 8-11, size of image file, temporary value, will update once written data from image
- 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
- bw.Write(posOfImageData) ' byte 12-15, offset of image data
- img.Save(ms, System.Drawing.Imaging.ImageFormat.Png) ' write image data to stream
- Dim imageSize = CInt(ms.Position) - posOfImageData ' current position subtract start of image data is size
- ms.Seek(posOfSizeData, System.IO.SeekOrigin.Begin) ' seek back to pos of size data
- bw.Write(imageSize) ' write actual image size
- ms.Seek(0, System.IO.SeekOrigin.Begin) 'seek back to start
- Return New Icon(ms) 'create new icon
- End Using
- End Using
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement