How to Configure Many to Many Relationship Using Entity Framework Fluent API

Selecting the size of a System.Drawing.Icon?

This is a fairly painful limitation in the ResourceManager class. Its GetObject() method doesn't provide a way to pass extra arguments that would allow selecting the returned icon by size. A workaround is to add the icon to the project instead. Use Project + Add Existing Item, select your .ico file. Select the added icon and change the Build Action property to "Embedded Resource".

You can now retrieve the desired icon with code like this:

    public static Icon GetIconFromEmbeddedResource(string name, Size size) {
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var rnames = asm.GetManifestResourceNames();
var tofind = "." + name + ".ICO";
foreach (string rname in rnames) {
if (rname.EndsWith(tofind, StringComparison.CurrentCultureIgnoreCase)) {
using (var stream = asm.GetManifestResourceStream(rname)) {
return new Icon(stream, size);
}
}
}
throw new ArgumentException("Icon not found");
}

Sample usage:

        var icon1 = GetIconFromEmbeddedResource("ARW04LT", new Size(16, 16));
var icon2 = GetIconFromEmbeddedResource("ARW04LT", new Size(32, 32));

Beware one possible failure mode: this code assumes that the icon was added to the same assembly that contains the method.

How Do I Create a System.Drawing.Icon with Multiple Sizes/Images?

An .ico file can have multiple images in it, but when you load an .ico file and create an Icon object, only one of those images is loaded. Windows chooses the most appropriate image based on the current display mode and system settings and uses that one to initialize the System.Drawing.Icon object, ignoring the others.

So you can't create a System.Drawing.Icon with multiple images, you have to pick one before you create the Icon object.

Of course, It is possible to create an Icon at runtime from a bitmap, is that what you are really asking? Or are you asking how to create an .ico file?

C# - WPF - Project size after adding icons (load icon from resources or pull from project EXE)

The icon which you add in the project properties is added to the executable's resources (.rsrc section) with identifier 32512 (IDI_APPLICATION). If you don't like relying on Icon.ExtractAssociatedIcon, you can use a more straightforward way of extracting it — using LoadIcon function. It isn't wrapped in .NET though, so there's more code:

IntPtr hIcon = LoadIcon(GetModuleHandle(null), new IntPtr(32512));
notifyIcon.Icon = Icon.FromHandle(hIcon);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon (IntPtr hInstance, IntPtr iconName);
[DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle (string moduleName);

See Using the same icon for the .exe and a form in a Windows Forms application without duplicating it? question for more details.

WPF includes methods for converting icons back and forth between HICON and ImageSource, so if you want to display the icon on the form, for example, you can do it too (see Imaging.CreateBitmapSourceFromHIcon etc.).

I see no reason to bother with this though if Icon.ExtractAssociatedIcon works fine. There may be some issues though, this function may extract not all icons, but only specific sizes.

See also Avoiding duplicate icon resources in a .NET C# project question.

Windows chooses wrong icon from multi-icon file and self renders to correct size

Both responses are close, but contain a subtle poison. You should not hardcode the requested size as 16x16.

Instead, query SystemInformation.SmallIconSize to determine the appropriate dimensions. Although the default is certainly 16x16, this could be changed by various things, such as DPI scaling.

See the MSDN article for more information on this property.

An example of usage would be

notifyIcon.Icon = new System.Drawing.Icon(this.Icon, SystemInformation.SmallIconSize),

System.Drawing.Icon constructor throwing Operation completed successfully exception

Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.



Related Topics



Leave a reply



Submit