Click to See Complete Forum and Search --> : Reduce file size - reuse icon
gleesonger
January 24th, 2009, 09:12 AM
Ive written a small application which is about 130kb however when I apply the same icon to each form it increases the file size by the number of forms times the size of the icon to about 500kb.How do I only include the icon once and have that one icon reused?
I'm using only one icon for everything executable,forms,system tray.
Thanks Ger.
nabeelisnabeel
January 26th, 2009, 01:28 AM
Lets c if my opinion works for you or not...
Instead of assigning icons in DESIGN mode of visual studio, attach the icons to forms at RUNTIME with statements like this (I dont remember the correct property right now)
this.Icon = "some icon path OR resource file path"
button1.Icon = "same path as above"
toolbar1.Icon = "same path as above"
Kindly tell us if it solves your problem ?
BigEd781
January 26th, 2009, 02:22 AM
How about simply making it an embedded resource instead. You can assign that at design time.
gleesonger
January 27th, 2009, 10:37 AM
I looked at embedding the icon as a resource but as the icon is the largest portion of the file's size I felt it a waste to have the icon 'embedded' twice.By twice I mean as the files executable icon and as an easily assessable resource so I had a look at some win32 api and came up (modified some code found online) with code that will use the current files icon as the forms icon.Ill post the code in the next post.
gleesonger
January 27th, 2009, 10:38 AM
GlobalVar.MainIcon = IconRetriever.GetFileIcon(System.Windows.Forms.Application.ExecutablePath, IconSize.Large);
...
form1.Icon = GlobalVar.MainIcon;
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace Hotkeys
{
public enum IconSize
{
Small,
Large,
}
public class IconRetriever
{
public static Icon GetFileIcon(string filePath, IconSize size)
{
Icon icon = null;
try
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr iconHandle;
if (size == IconSize.Small)
{
iconHandle = Win32.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
}
else if (size == IconSize.Large)
{
iconHandle = Win32.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
}
icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
catch (Exception e)
{
}
return icon;
}
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
private class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
public const uint SHGFI_SMALLICON = 0x1;
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint uFlags);
}
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.