Using a Windows Forms icon in WPF - wpf

I've got this WPF code which works...
Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);
I'm using a windows forms notifyIcon control in my WPF app, and I now need to assing the Icon to it. How do I get from my WPF icon to a System.Drawing.Icon ?

I use the following method:
// Setup the new NotifyIcon
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Text = "Name of Application";
notifyIcon.Icon = new System.Drawing.Icon("media/images/VS.ico");
notifyIcon.Visible = true;
Make sure you add a reference to System.Drawing.

Imaging.CreateBitmapSourceFromHBitmap
I use it like:
return Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
where source is a Bitmap, which you can get by calling your Icon's .ToBitmap() method.

Related

WPF: put an icon on a radWindow programatically

I want to put an icon on the top left of a radWindow programatically
my code is like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("../ressources/enregistrer.png", UriKind.Relative))
};
radWindow.Show();
but the icon dont show up
does anyone have an idea ?
EDIT
This is the architecture on my project:
The file from where the above code is taken is circled in red
The ressource file is circled in green
<telerik:RadWindow.Icon>
<Image Source="pack://application:,,,/ressources/enregistrer.png" Height="18"/>
</telerik:RadWindow.Icon>
// this seems to work. You will need to provide a size.
I dont know why my solution above did not work but I found a workaround like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("pack://application:,,,/ressources/enregistrer.png", UriKind.RelativeOrAbsolute))
}
radWindow.Show();

How to remove the border of a Button in code?

I am developing a windows phone 7 application. I am new to windows phone 7 applications. In my application I have created a button control dynamically and added the background image to the button control as follows.
Button AlphabetButton = new Button();
AlphabetButton.Content = vAlphabet;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
//brush.Stretch = Stretch.None;
AlphabetButton.Background = brush;
AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);
AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
AlphabetButton.Width = 80;
AlphabetButton.Height = 80;
I want to remove the border of the button control because with that border the image does not appear as per requirement. How to do this? Can we do this with the BorderThickness attribute of button control or is there any other way? Can you please provide me any code or link through which I can resolve the above issue? If I am doing anything wrong then please guide me.
The easiest way is to set the BorderThickness to 0 like for Example:
Button alphabetButton = new Button();
alphabetButton.BorderThickness = new Thickness(0.0);
Another option could be to set the BorderBrush to Transparent or to change the whole Style of the button and especially its ControlTemplate.
I think Button.StrokeTHickness is the correct property to adjust the borders.

How to programmatically create a WPF window in a WinForm application

I have an existing WinForm app which is too much to port to WPF right now.
However, I need a window with some tricky transparency behavior that I cannot achieve in a WinForm (yes, tried Layerd Windows but it's a no-go).
WPF allows the transparency behavior I need beautifully and simply.
I googled of course, but can only find hints how to create a WPF control within a WinForm but that is NOT what I need. I need a separate WPF window that is completely independant of my other Forms.
The WPF window will be a rather simple full-screen and borderless overlay window where I will do some simple drawings, each with different transparencies.
How can I create a WPF window within a WinForm application?
Add the necessary WPF references to your project, create a WPF Window-instance, call EnableModelessKeyboardInterop and show the window.
The call to EnableModelessKeyboardInterop makes sure, that your WPF window will get keyboard inputs from your Windows Forms app.
Take care, if you open a new Window from within your WPF window, the keyboard input will not be routed to this new window. You have to call also for these newly created windows EnableModelessKeyboardInterop.
Fore your other requirements, use Window.Topmost and Window.AllowsTransparency. Don't forget to set the WindowStyle to None, otherwise, transparency is not supported.
Update
The following references should be added to use WPF in your windows forms application:
PresentationCore
PresentationFramework
System.Xaml
WindowsBase
WindowsFormsIntegration
Here's the (tested) solution. This code can be used in both a WinForm or a WPF app.
No XAML needed at all.
#region WPF
// include following references:
// PresentationCore
// PresentationFramework
// WindowsBase
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
#endregion
public class WPFWindow : Window
{
private Canvas canvas = new Canvas();
public WPFWindow()
{
this.AllowsTransparency = true;
this.WindowStyle = WindowStyle.None;
this.Background = Brushes.Black;
this.Topmost = true;
this.Width = 400;
this.Height = 300;
canvas.Width = this.Width;
canvas.Height = this.Height;
canvas.Background = Brushes.Black;
this.Content = canvas;
}
}
The window background is fully transparent.
You can draw on the canvas and each element can have it's own transparency (which you can determine by setting the alpha channel of the Brush used to draw it).
Simply invoke the window with something like
WPFWindow w = new WPFWindow();
w.Show();

How to set uri for local image in silverlight app?

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:
StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyLib;component/my.png", UriKind.Relative));
BitmapImage image = new BitmapImage();
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;
But it's not woking. I think it's the Uri not set correctly. Help please.
This works:-
BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;
I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.
Do you have your image marked as "Resource" in the properties window, or "Content"?
You could always set a style as a resource in your application and then call it like:
Application.Current.Resources["myCoolStyle"] and apply that to the image.

WPF - Refer to app icon in code

i have added an icon to a WPF app in the project properties.
How do I refer to that icon so I can add it to a
NotifyIcon that I am creating for system tray.
In code that is??
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("MyIcon.ico");
Does not work.
Malcolm
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = System.Drawing.Icon.ExtractAssociatedIcon(
System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name);
ni.Visible = true;
I think this may be what you are looking for:
How do I use an icon that is a resource in WPF?
You need to embed the icon as a resource and then you can access it from code.

Resources