Since I am not getting anywhere with my previous question, I would like to know, are there any ways I can create icons on the fly on WPF?
Asked
Active
Viewed 2,249 times
4
-
What do you mean by 'on the fly'? – Emond May 11 '11 at 14:58
-
he probably means generating an image using xaml (likely data driven), and saving it as a bitmap – Joel Martinez May 11 '11 at 15:01
-
I mean on demand, Lets say I want to display progress status in Tray icon, rather displaying tool tip, I want to display an icon with display status on top the icon. – iraSenthil May 11 '11 at 15:07
5 Answers
1
You don't need WPF.
Usign GDI+ (System.Drawing.dll
), you can create a 16x16 Bitmap
, then call Icon.FromHandle(bitmap.GetHicon())
.

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
-
-
1Why take an additional dependency on System.Drawing and do expensive WPF/GDI interop if you can do it natively in WPF only. – bitbonk May 11 '11 at 15:24
-
@bitbonk: In his original question, it sounded like he wanted GDI+. WPF doesn't have a NotifyIcon, so he needs GDI+ and WinForms anyway. – SLaks May 11 '11 at 15:26
-
-
Ooops, I didn't see he actually wants a NotityIcon. This will involve win32 interop in any case, I think. – bitbonk May 11 '11 at 15:35
0
You can use WritableBitmap for this.

bitbonk
- 48,890
- 37
- 186
- 278
-
@iraSenthil here is a per pixel drawing method http://tipsandtricks.runicsoft.com/CSharp/WpfPixelDrawing.html – Code0987 May 15 '11 at 18:07
-
0
You can use Taskbar icon progress bar...Sure U have seen, most of the application if doing any scanning or progress things, it displays progress actions on Icon.
Do this in your main form where u included the Icon
<Window x:Class="CCTrayHelper.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="/CCTrayHelper;component/Images/CCTrayHelperIcon.png">
<Window.TaskbarItemInfo>
<TaskbarItemInfo />
</Window.TaskbarItemInfo>
Trig it from code behind
private void OnProgress(object sender, EventArgs args)
{
Dispatcher.Invoke(DispatcherPriority.Send, (Action)delegate() { TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None; });
// Use here your progress type
}

PawanS
- 7,033
- 14
- 43
- 71
-
-
Yes!! it works only for Windows 7......... @iraSenthil, u means something Like Messenger Icon where if user is busy or not available then it shows some other animation or image over Icon.. right?? – PawanS May 12 '11 at 07:10
-
0
This is what I ended up doing, I don't fully understand the details, if you find anything that can be improved, please do comment. Thanks for all the answers and comments.
public static ImageSource GetIconWithText(int digit)
{
BitmapImage myBitmapImage = new BitmapImage(new Uri(@"Images\PomoDomo.ico",
UriKind.RelativeOrAbsolute));
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
// Draw image
drawingContext.DrawImage(myBitmapImage, new Rect(0, 0, myBitmapImage.Width,
myBitmapImage.Height));
var typeFace = new Typeface(new FontFamily("Verdana"), FontStyles.Normal,
FontWeights.ExtraBold, FontStretches.UltraCondensed);
var formatedText = new FormattedText(digit.ToString(),
CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
typeFace,
40,
System.Windows.Media.Brushes.White);
//Center the text on Image
int pointY = (int)(myBitmapImage.Height - formatedText.Height) / 2;
int pointX = (int)(myBitmapImage.Width - formatedText.Width) / 2;
drawingContext.DrawText(formatedText, new Point(pointX, pointY));
}
RenderTargetBitmap finalBitmap = new RenderTargetBitmap((int)myBitmapImage.Width,
(int)myBitmapImage.Height, myBitmapImage.DpiX, myBitmapImage.DpiY,
PixelFormats.Pbgra32);
finalBitmap.Render(drawingVisual);
return finalBitmap;
}
private static void SaveImage(RenderTargetBitmap returnBitmap, string pngFileName)
{
string fileName = string.Format("{0}.png", pngFileName)
PngBitmapEncoder image = new PngBitmapEncoder();
image.Frames.Add(BitmapFrame.Create(returnBitmap));
using (Stream fs = File.Create(fileName))
{
image.Save(fs);
}
}

iraSenthil
- 11,307
- 6
- 39
- 49