Can I create icons on the fly in WPF?

Since I am not getting a single place with the previous question , I would like to know if there are ways to create icons on the fly in WPF?

+4
source share
5 answers

This is what I finished, I do not quite understand the details, if you find something that can be improved, please make a 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); } } 
0
source

You do not need WPF.

To arrange GDI + ( System.Drawing.dll ), you can create 16x16 Bitmap and then call Icon.FromHandle(bitmap.GetHicon()) .

+1
source

You can use WritableBitmap for this.

0
source

You can use the taskbar icon execution bar ... Of course, U has seen most of the application, if it performs any scan or progress operations, it displays the progress actions in the icon.

Do it 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> 

Run it from code

  private void OnProgress(object sender, EventArgs args) { Dispatcher.Invoke(DispatcherPriority.Send, (Action)delegate() { TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None; }); // Use here your progress type } 
0
source

Here you can find a way here to make text on writeablebitmap

0
source

All Articles