【问题标题】:Can I create Icons on the fly in WPF?我可以在 WPF 中动态创建图标吗?
【发布时间】:2011-08-23 09:41:52
【问题描述】:

由于我以前的question 没有任何效果,我想知道,有什么方法可以在 WPF 上即时创建图标?

【问题讨论】:

  • 你所说的“即时”是什么意思?
  • 他可能是指使用 xaml 生成图像(可能是数据驱动),并将其保存为位图
  • 我的意思是按需,假设我想在托盘图标中显示进度状态,而不是显示工具提示,我想在图标顶部显示一个带有显示状态的图标。

标签: c# .net wpf gdi


【解决方案1】:

你不需要 WPF。

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

【讨论】:

  • 你是说我在 WPF 中做不到?
  • 否;我是说你可以在 WinForms 中做到这一点。
  • 如果您只能在 WPF 中进行原生操作,为什么还要额外依赖 System.Drawing 并进行昂贵的 WPF/GDI 互操作。
  • @bitbonk:在他最初的问题中,听起来他想要 GDI+。 WPF 没有 NotifyIcon,所以无论如何他都需要 GDI+ 和 WinForms。
  • @SLaks,我正在 WPF 中寻找解决方案。
【解决方案2】:

您可以为此使用WritableBitmap。

【讨论】:

【解决方案3】:

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

在包含图标的主窗体中执行此操作

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

【讨论】:

  • 我需要托盘图标上的状态,无论如何感谢您的建议。
  • 是的!它仅适用于 Windows 7 ......@iraSenthil,你的意思是像 Messenger 图标,如果用户很忙或不可用,那么它会在图标上显示一些其他动画或图像.. 对吗??
  • 更接近,但我想添加一些文本而不是一些有限的图标。
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    你可以找到 here 和 here 的方法在 writeablebitmap 上渲染文本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2021-05-19
      • 1970-01-01
      相关资源
      最近更新 更多