4

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
iraSenthil
  • 11,307
  • 6
  • 39
  • 49

5 Answers5

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
0

You can use WritableBitmap for this.

bitbonk
  • 48,890
  • 37
  • 186
  • 278
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
  • I need status on tray icon, anyway thanks for the suggestion. – iraSenthil May 12 '11 at 04:15
  • 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
  • Closer, But I want to add some text instead of some limited icons. – iraSenthil May 12 '11 at 15:00
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
0

You can find a method here and here to render text on writeablebitmap

Community
  • 1
  • 1
Code0987
  • 2,598
  • 3
  • 33
  • 51