将BitmapImage转换为图标而不保存到磁盘中



Iam使用此代码从字符串生成位图。现在我需要将此位图转换为ICO或图标,下面的方法不起作用,当我使用System.Drawing.Imaging.ImageFormat.icon时会导致错误。错误为Null Refence Exception。如果我使用任何其他格式,错误已解决,并且m_notifyIcon中出现了新的错误,无法将其用作图像。该怎么办…

        System.Drawing.Bitmap bmp = TextToBitmap("This");//return bitmap 
        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Icon);
        BitmapImage imgg = new BitmapImage();
        imgg.BeginInit();
        imgg.StreamSource = new MemoryStream(ms.ToArray());
        imgg.EndInit();
        //img.Source = imgg;
        Icon io = new System.Drawing.Icon(ms);
        m_notifyIcon.Icon = io;

我的问题得到了答案。

        System.Drawing.Bitmap bmp = TextToBitmap("String");
        System.IntPtr ich = bmp.GetHicon();
        System.Drawing.Icon io = System.Drawing.Icon.FromHandle(ich);
        m_notifyIcon.Icon = io;

获取位图iamage使用IntPtr并使用上面的代码。。。

最新更新