EDN Admin
Well-known member
Hi ,
I want to set button image by using an icon file from different assembly. I have tried the below piece of code. It is giving the result
<pre class="prettyprint int size = 32;
//Method to Extract Icon from the assembly
this.ExtractImagesFromAssembly("ClassLibrary1.dll", "Text_Document.ico", size);
private void ExtractImagesFromAssembly(string assemblyPath, string imageName, int size)
{
Assembly assembly = Assembly.LoadFrom(assemblyPath);
assembly = Assembly.ReflectionOnlyLoad(assembly.FullName);
string name = assembly.GetName().Name + "." + imageName;
using (Stream stream = assembly.GetManifestResourceStream(name))
{
try
{
Size imageSize = new Size(size, size);
//Creating the icon with specific size
Icon icon = new Icon(stream, imageSize);
this.button1.Image = icon.ToBitmap(); ;
}
catch (ArgumentException)
{
stream.Position = 0;
}
}
}
[/code]
here the size is the size of the icon. The drawback of the above code is I am reading the icon file as stream and recreating the icon file, instead of using the existing one. and I am using the icon file as an embedded resource. I want to use it as
a resource.
Anyone please suggest me to load the icon file without recreating the same. If I can get the uri of the icon file , I could have to try the below piece of code. I am not sure whether this idea will work; it is working in WPF
<pre class="prettyprint /// <summary>
/// Retrieve ImageSource of icon sized to iconSize
/// </summary>
/// <param name="iconUri The pack URI for a resource file that is compiled into a local or referenced assembly</param>
/// <param name="iconSize Size of icon, defined by IconSize enumeration</param>
/// <returns>ImageSource of icon, sized to iconSize</returns>
public static ImageSource GetImageSource(string iconUri, int iconSize)
{
Uri uri;
if (Uri.TryCreate(iconUri, UriKind.RelativeOrAbsolute, out uri))
{
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
// Return the first frame within the icon set whose width matches the desired icon size
var result = decoder.Frames.SingleOrDefault(f => f.Width == iconSize);
// If the default is returned for a lack of the desired size, pick the first frame after ordering them in increasing width
if (result == default(BitmapFrame))
result = decoder.Frames.OrderBy(f => f.Width).First();
return result;
}
else
{
return null;
}
}[/code]
<br/>
View the full article
I want to set button image by using an icon file from different assembly. I have tried the below piece of code. It is giving the result
<pre class="prettyprint int size = 32;
//Method to Extract Icon from the assembly
this.ExtractImagesFromAssembly("ClassLibrary1.dll", "Text_Document.ico", size);
private void ExtractImagesFromAssembly(string assemblyPath, string imageName, int size)
{
Assembly assembly = Assembly.LoadFrom(assemblyPath);
assembly = Assembly.ReflectionOnlyLoad(assembly.FullName);
string name = assembly.GetName().Name + "." + imageName;
using (Stream stream = assembly.GetManifestResourceStream(name))
{
try
{
Size imageSize = new Size(size, size);
//Creating the icon with specific size
Icon icon = new Icon(stream, imageSize);
this.button1.Image = icon.ToBitmap(); ;
}
catch (ArgumentException)
{
stream.Position = 0;
}
}
}
[/code]
here the size is the size of the icon. The drawback of the above code is I am reading the icon file as stream and recreating the icon file, instead of using the existing one. and I am using the icon file as an embedded resource. I want to use it as
a resource.
Anyone please suggest me to load the icon file without recreating the same. If I can get the uri of the icon file , I could have to try the below piece of code. I am not sure whether this idea will work; it is working in WPF
<pre class="prettyprint /// <summary>
/// Retrieve ImageSource of icon sized to iconSize
/// </summary>
/// <param name="iconUri The pack URI for a resource file that is compiled into a local or referenced assembly</param>
/// <param name="iconSize Size of icon, defined by IconSize enumeration</param>
/// <returns>ImageSource of icon, sized to iconSize</returns>
public static ImageSource GetImageSource(string iconUri, int iconSize)
{
Uri uri;
if (Uri.TryCreate(iconUri, UriKind.RelativeOrAbsolute, out uri))
{
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
// Return the first frame within the icon set whose width matches the desired icon size
var result = decoder.Frames.SingleOrDefault(f => f.Width == iconSize);
// If the default is returned for a lack of the desired size, pick the first frame after ordering them in increasing width
if (result == default(BitmapFrame))
result = decoder.Frames.OrderBy(f => f.Width).First();
return result;
}
else
{
return null;
}
}[/code]
<br/>
View the full article