Icon problem
Hi all,
I have been on this problem for ages! Im trying to create an icon yet am getting some errors. I think shows that the problem lies because I use the Compact Framework. I have read that the Compact Framework only supports the Icon constructors that accept Stream objects. Therefore, I need to create a Stream object with the file name that I wish to open and pass this to the Icon constructor instead. Can anyone guide me on how to code this? As I am new to System.IO.File & FileStream classes as well as the Icon class.
The code I have is shown below..
Thanks!
K9.
//Code Begin
VectorStyle
Style =newVectorStyle(newIVertexStyle[] {newIconVertexStyle(newIcon(SAMPLE_DATA_DIR +"..\\..\\Icons\\Lib.ico").ToBitmap())},null,null);styles.Add(Style);
//where SAMPLE_DATA_DIR ....
privateconststring SAMPLE_DATA_DIR ="C:\\Program Files\\Shape\\";
//Code End
[1964 byte] By [
kinpin9] at [2007-12-23]
1) You cannot construct an icon from a file path. Instead of having
new Icon(SAMPLE_DATA_DIR + "..\\..\\Icons\\Lib.ico");
You must use:
new Icon(File.OpenRead(SAMPLE_DATA_DIR + "..\\..\\Icons\\Lib.ico"));
2) Compact Framework Icon object does not have ToBitmap method. You can use the following to convert:
Bitmap bm = new Bitmap(icon.Width, icon.Height);
Graphics g = Graphics.FromImage(bm);
g.Clear(MyFavoriteBackgroundColor);
g.DrawIcon(icon, 0, 0);
g.Dispose();
Not to mention relative paths and drive letters you're using are not going to work on devices.
If you're not working with devices and NETCF, please make sure to pick correct forum next time.
Hi I have now changed the code so it looks like the following (shown below), however I recieve two errors that are shown below..
Error 1
The best overloaded method match for 'MapClassLib.Style.IconVertexStyle.IconVertexStyle(System.Drawing.Image)' has some invalid arguments
Error 2
Argument '1': cannot convert from 'System.Drawing.Icon' to 'System.Drawing.Image'
VectorStyle
gpsStyle = new VectorStyle(new IVertexStyle[] { new IconVertexStyle( new Icon(File.OpenRead(SAMPLE_DATA_DIR + "..\\..\\Icons\\Lib.ico")))},null, null);Bitmap bm = new Bitmap(Icon.Width, Icon.Height);Graphics g = Graphics.FromImage(bm);g.DrawIcon(Icon, 0, 0);g.Dispose();
where....
private const string SAMPLE_DATA_DIR ="C:\\Program Files\\Graticule\\MapClassLib\\Sample Data\\Shape\\";Im not sure I have fully understood what to do as I am recieving the same errors, what did you mean by (2) ..you can use the following to convert?
I apologise for my lack of understanding.
Thanks
Hi I have now changed the code so it looks like the following (shown below), however I recieve two errors that are shown below..
Error 1
The best overloaded method match for 'MapClassLib.Style.IconVertexStyle.IconVertexStyle(System.Drawing.Image)' has some invalid arguments
Error 2
Argument '1': cannot convert from 'System.Drawing.Icon' to 'System.Drawing.Image'
VectorStyle
gpsStyle = new VectorStyle(new IVertexStyle[] { new IconVertexStyle( new Icon(File.OpenRead(SAMPLE_DATA_DIR + "..\\..\\Icons\\Lib.ico")))},null, null);Bitmap bm = new Bitmap(Icon.Width, Icon.Height);Graphics g = Graphics.FromImage(bm);g.DrawIcon(Icon, 0, 0);g.Dispose();
where....
private const string SAMPLE_DATA_DIR ="C:\\Program Files\\Graticule\\MapClassLib\\Sample Data\\Shape\\";Im not sure I have fully understood what to do as I am recieving the same errors, what did you mean by (2) ..you can use the following to convert?
I apologise for my lack of understanding.
Thanks
The idea was that you use the conversion code I provided to get from icon to bitmap:
Icon icon = new Icon(File.OpenRead(SAMPLE_DATA_DIR + "..\\..\\Icons\\Lib.ico"));
Bitmap bm = new Bitmap(Icon.Width, Icon.Height);
Graphics g = Graphics.FromImage(bm);
g.DrawIcon(icon, 0, 0);
g.Dispose();
VectorStyle gpsStyle = new VectorStyle(new IVertexStyle[] {
new IconVertexStyle( bm )},null, null);And of course SAMPLE_DATA_DIR cannot have C: in it - on the device there are no drive letters