VB.Net WPF : Define BitmapImage with Resources - wpf

I'm trying to use a RadDesktopAlert from Telerik and I need to set an Icon to it.
So, I did this :
Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri("pack://application:,,,/Resources/questions1.png")
bi.EndInit()
alert.Icon = bi
alert.IconColumnWidth = 75
But my alert don't have any icon, it sounds like that it can't find the resource... Build action is set to "Resource" for my file.
Can I have help please ?
Thanks !

This should work, provided that the image file is in folder "Resources" in your Visual Studio project, and that its Build Action is set to Resource:
Dim bitmap As New BitmapImage(New Uri("pack://application:,,,/Resources/questions1.png"))
Dim image As New Image() With {.Source = bitmap}
alert.Icon = image

Related

Set icon for MenuItem VB

i need to create the context menu from code behind in WPF.
Everything works great except the Icon, i set the MenuItem icon like this
Dim tsmi As New MenuItem() With {
.Header = cmd.Name,
.Icon = cmd.Icon,
.Tag = cmd
}
where cmd.Icon is a System.Drawing.Image.
What i get instead of the Icon is the string System.Drawing.Image where it should be the Image.
Can anyone help?
System.Drawing.Image is from WinForms, what you need is a System.Windows.Controls.Image.
You can make one like this:
New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/Your.Assembly.Name;component/Images/image.png"))}
...where you have a file called image.png (marked with Build Action=Resource) in a folder Images in the assembly Your.Assembly.Name.dll.
The MenuItem documentation shows this XAML:
<MenuItem Header="New">
<MenuItem.Icon>
<Image Source="data/cat.png"/>
</MenuItem.Icon>
</MenuItem>
So you can clearly use a WPF Image control for the icon. The documentation for the Image.Source property provides a link to a topic entitled "How to: Use the Image Element" and it includes this code example:
' Create Image Element
Dim myImage As New Image()
myImage.Width = 200
' Create source
Dim myBitmapImage As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit()
myBitmapImage.UriSource = New Uri("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg")
' To save significant application memory, set the DecodePixelWidth or
' DecodePixelHeight of the BitmapImage value of the image source to the desired
' height or width of the rendered image. If you don't do this, the application will
' cache the image as though it were rendered as its normal size rather then just
' the size that is displayed.
' Note: In order to preserve aspect ratio, set DecodePixelWidth
' or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200
myBitmapImage.EndInit()
'set image source
myImage.Source = myBitmapImage
That pretty much gives you everything you need. I have never used any of these types or members before. I just spent some time reading the relevant documentation.

WPF loading image from internet

Hey all i am trying to find a way to load an image via the WebClient using a WFP window.... The code below loads just fine when i am using a normal Windows Form:
Dim wc As New Net.WebClient
picNextTopic1.Image = Image.FromStream(wc.OpenRead(theAPI.ImgURL(2).Replace("{width}", "50").Replace("{height}", "50")))
However, this doesnt seem to work for WPF???
The WPF seems to use a .source for images? How can i convert that code above in order to use it with a WPF window?
WPF is not Windows Forms. You need to build WPF applications completely different. Properties, INotifyPropertyChanged etc.
But to your problem:
Dim wc As New WebClient()
Dim bytes = wc.DownloadData("http://....")
Dim ms = New MemoryStream(bytes)
Dim img = New BitmapImage()
ms.Seek(0, SeekOrigin.Begin)
img.BeginInit()
img.StreamSource = ms
img.EndInit()
picNextTopic1.Source = img

SlimDX rendering a visual to a bitmap

I want to render a viewport3d visual to an image using hardware rendering. How could I do that using SlimDX? I'm using WPF & VB.NET 4.0 and SlimDX. Any tips will help, thanks in advance. :)
Here is the code I use to render a viewport3d, it is very slow and gives bad results:
//rendering viewport3D to image
Dim viewportPlate As New RenderTargetBitmap(600, 600, 96 , 96 , PixelFormats.Pbgra32)
viewportPlate.Render(viewport3d)
//path to save
Dim path As String = imgSave
Dim fs As FileStream = New FileStream(path, FileMode.Create)
//encoding to PNG and saving
Dim encoder As BitmapEncoder = New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(viewportPlate))
encoder.Save(fs)
PS: If anyone has a better idea on how to do this using something other than SlimDX I will welcome those too...

Using a Windows Forms icon in 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.

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.

Resources