我可以在 WPF 中即时创建图标吗?

发布于 2024-11-06 16:09:27 字数 139 浏览 1 评论 0原文

由于我之前的问题没有取得任何进展,我我想知道,有什么方法可以在 WPF 上动态创建图标吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾`听者〃 2024-11-13 16:09:27

你不需要WPF。

使用GDI+(System.Drawing.dll),可以创建一个16x16的Bitmap,然后调用Icon.FromHandle(bitmap.GetHicon())

You don't need WPF.

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

浪荡不羁 2024-11-13 16:09:27

您可以使用 为此,可写位图

You can use WritableBitmap for this.

凉墨 2024-11-13 16:09:27

您可以使用任务栏图标进度条...当然您已经看到,大多数应用程序如果执行任何扫描或进度操作,它都会在图标上显示进度操作。

包含了图标Trig it

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

在您的主窗体中执行此操作,其中您从代码后面

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

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
    }
叫思念不要吵 2024-11-13 16:09:27

这就是我最终所做的,我不完全理解细节,如果您发现任何可以改进的地方,请发表评论。感谢所有的答案和评论。

 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);
     }
 }

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);
     }
 }
演多会厌 2024-11-13 16:09:27

您可以找到一种方法 此处此处渲染文本可写位图

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文