WPF button using System.Windows.Forms.ImageList - c#

is there a nice way to do the following. Get a WPF button and a Windows.Forms.ImageList and use them together. Here is the code:
<Button Name="SOMETHING" Content="Button" Height="23" VerticalAlignment="Top" Width="75"/>
System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList();
string str_directory = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
this.imgList.Images.Add(System.Drawing.Image.FromFile(str_directory + "\\Resources\\AboutImage.png"));
WPFbutton.Background = imgList.Images[0];
I am tyring to get the Windows.Forms ImageList and use it on a WPF button. Is there a nice way to fix it?

There is no need for an ImageList. Just do it as shown in the code below, which assumes that the path "Resources\AboutImage.png" is relative to the application's current working directory.
Apparently you've called Path.GetDirectoryName two times to cut off the "bin\Debug\" or "bin\Release\" part of the path and thus access the image file directly from the Visual Studio project structure. This will not work when the application is deployed somewhere else. Instead, set the Build Action of the image file to Content, and Copy to Output Directory to Copy always or Copy if newer.
var path = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "AboutImage.png");
var bitmap = new BitmapImage(new Uri(path));
WPFbutton.Background = new ImageBrush(bitmap);
However, a far better approach would be to load an image resource directly. Set the Build Action to Resource and load the image by a WPF Pack URI:
var bitmap = new BitmapImage(
new Uri("pack://application:,,,/Resources/AboutImage.png"));
WPFbutton.Background = new ImageBrush(bitmap);
Besides that, you would usually set the Background in XAML, like:
<Button ...>
<Button.Background>
<ImageBrush ImageSource="/Resources/AboutImage.png"/>
</Button.Background>
</Button>

Related

Cannot play a relative mp4 video in a MediaElement WPF

I'm trying to play a mp4 video in my media element in WPF.
The mp4 video is in a folder in my solution.
I tried diffrent things but the only way it works is when i put the full path to the video in the uri. What am i doing wrong?
XAML
<MediaElement x:Name="VideoDice" Grid.Row="2" Grid.Column="2" LoadedBehavior="Manual" MediaEnded="VideoDice_MediaEnded" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
C#
VideoDice.Source = new Uri("DiceMovies/Dice_2.mp4", UriKind.Relative);
VideoDice.Height = 500;
VideoDice.Width = 500;
VideoDice.Play();
In your project for the Dice_2.mp4 file it's necessary to set Copy to Output Directory property to Copy if newer. Looks like your .mp4 file does not copied to the output directory automatically.

How to set the background of a WPF grid to an image in local folder?

I'm developing a small WPF application and I'm trying to change the background of a grid to an image the user chooses (stored somewhere on the computer on a different location from the app). Is it possible without having the images included on the project and marked as a Resource? How?
Assuming you Grid name is grid, then xaml would be:
<Grid Name="grid">
...
</Grid>
then to set an image programatically to grid, you should use the following code snippet:
string imgPath=#"E:\anImage.jpg";
grid.Background= new ImageBrush { ImageSource = new BitmapImage(new Uri(imgPath,
UriKind.RelativeOrAbsolute)) };
you just need to set the image's source URI to the image location
<Image Source="<<URI of image>>"/>
<ImageBrush ImageSource="<<URI of image>>"/>
or you can do the same via binding to allow it to be customised
<Image Source="{Binding Data}"/>
in this example Data is a byte[] stored in the model, but could be anything that converts to an image source
This works for me :
1) Add image in solution (Add => Existing Item)
2)<Grid>
<Grid.Background>
<ImageBrush ImageSource="/App;component/Chrysanthemum.jpg">
</ImageBrush>
</Grid.Background>
</Grid>

WPF - Uri to content images

I spent 1 hour on browsing internet and trying to find a way how to load Uri to content image and create an BitmapImage.
That is what I have:
{
Uri x = new Uri("/Images/appbar.chevron.up.png", UriKind.Relative);
BitmapImage bi = new BitmapImage(x);
return x;
}
Images in Solution Explorer are just under project:
And each image have Build Action as Content
I'm recieving The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details. on creating BitmapImage, but debugger showes me that Uri was created:
Please tell me, how can I load selected image?
As noted, to load the image from your app package you'll use the ms-appx protocol. See the URI schemes documentation on MSDN
Uri x = new Uri("ms-appx:///Images/appbar.chevron.up.png");
BitmapImage bi = new BitmapImage(x);
Then you'll want to return the BitmapImage rather than the URI to your caller:
return bi; // not: return x;
Or set it on an Image control. The BitmapImage is just data and doesn't show up on its own.
img.SetSource(bi);
Where img is created in your Xaml:
Guessing from the names of the images you are trying to set them in appbar buttons. For that you can use the Segoe UI Symbol font instead of loading images:
<AppBarButton>
<AppBarButton.Icon>
<FontIcon FontFamily="Segoe UI Symbol" Glyph=""/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton>
<AppBarButton.Icon>
<FontIcon FontFamily="Segoe UI Symbol" Glyph=""/>
</AppBarButton.Icon>
</AppBarButton>
Or if you want to set AppBarButtons to images in xaml:
<AppBarButton>
<AppBarButton.Icon>
<BitmapIcon UriSource="Images/appbar.chevron.up.png"/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton>
<AppBarButton.Icon>
<BitmapIcon UriSource="Images/appbar.chevron.down.png"/>
</AppBarButton.Icon>
</AppBarButton>
I don't know exactly how content and resources work in WinRT, but in WPF, "Content" items must be copied to the output directory. They get added to the manifest, but the files themselves are not embedded.
If you want to embed the file, use a build action of "Resource" (it's possible that WinRT does not even support "Content"--I have never used it). It also appears that you need to create an Absolute URI with ms-appx: scheme.

Pack Strings in WPF User Control Library

I had created a User Control in my main Project Originally. I have now created a new Project that uses the WPF User Control Library Template and moved my control there.
The issue I am having, is my control uses images as the content for buttons. I have moved the images along with the controls into the new project, but I cannot get the Pack URIs to work. The Control and the images both reside in the same folder of the project called MyControl an the new Project name is MyControls.
I have tried:
<Button Name="Button1" ToolTip="Button1" Click="Button1Action">
<Image Source="pack://application:,,,/MyControl/image1.png" />
</Button>
and
<Button Name="Button1" ToolTip="Button1" Click="Button1Action">
<Image Source="pack://application:,,,MyControls;/MyControl/image1.png" />
</Button>
I also tried adding the images to the Resources.resx file, then in the Code Behind converting that to a BitmapSource, creating an Image control, setting its source to the BitmapSource, and then setting the Button.Content to the Image. I assume that since the png has a transparent background, it was messed up in the process, it was displayed with a black background using this method.
The Build Action for the Image in the project is set to Resource, so I assume I am just missing the correct Pack string.
Your second pack uri was close, but you missed one word (and a slash)! It should be
<Image Source="pack://application:,,,/MyControls;component/MyControl/image1.png" />

How to find out relative path of image

In a legacy project there are absolute path to the images,
for example:
C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png
Now I want to use relative path, so that every developer can use that
project, no matter where he has his copy of the sourcecode.
Is there an easy way to find out the (Resource) relative path?
How can I use it then?
At the moment I have for example:
<Image Source="C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png" Stretch="Fill" />
What I want is something like:
<Image Source="arrow.png" Stretch="Fill" />
Tried around with
<Image Source="pack:,,, arrow.png" Stretch="Fill" />
<Image Source="/WPF1;arrow.png"></Image>
and similar things
Put the image files into a folder (named let's say Images) in your Visual Studio project and set their build action to Resource.
Now you can simply use them in XAML like this:
<Image Source="Images/arrow.png" ... />
In code-behind you would have to write
var uri = new Uri("pack://application:,,,/Images/arrow.png");
image.Source = new BitmapImage(uri);
Add image using Source property of the Image control by clicking
then the path will be something like this:
/[project name];component/[folder name: e.g. Images]/[file name]

Categories