I use Svg2Xaml to display SVG image but it does not shown in the WPF application.
Xaml Code
<Button Style="{StaticResource TransparentStyle}">
<Image Source="{svg:SvgImage /UIDebug;component/Images/logout.svg}" Height="30" Width="30" OpacityMask="White"/>
</Button>
DLL References
Import Library to Xaml
<Window ...
xmlns:svg="clr-namespace:Svg2Xaml;assembly=Svg2Xaml"></Window>
Build Action
The logout.svg image is stored in the /<Project_Root>/Images/logout.svg with Build Action equal to Resource.
Why?
Related
What I need :
In C# WPF on Visual studio 2019 in Windows 10, I want to show and play a video inside a My window from the app not directly path
What have I done :
I have included that video in my project, then I set the path for the video and it shows it to me in design view! :
but when I start my project , It won't show the video !
What have I tried and my code :
so I decided to try it in three-way, also to try to show a gif for sure about the path.
Embedded path
Normal Path
Direct path on windows
but only the direct path works! While the same embedded path works for GIF
MyGIF : Source="pack://application:,,,/Medias/1Correctmation.gif" => works fine
My Video : Source="pack://application:,,,/Medias/Video1.mp4" => not working
My Video : Source="C:\Users\user\Desktop\Video1.mp4" => workis fine but I don't want this directly path
also for my video file I tried these BuildAction:
Embedded
Resource
Content Resource
none of them work.
XAML:
<Grid>
<Label Content="Embeded Video :" HorizontalAlignment="Left" Margin="10,34,0,0" VerticalAlignment="Top" Width="100"/>
<MediaElement x:Name="Embd_Video1_mp4" Margin="115,10,499,309" Stretch="Fill" Source="pack://application:,,,/Medias/Video1.mp4"/>
<Label Content="Normal Path :" HorizontalAlignment="Left" Margin="10,172,0,0" VerticalAlignment="Top" Width="82"/>
<MediaElement x:Name="Normal_Video1_mp4" Margin="115,134,444,154" Stretch="Fill" Source="Medias/Video1.mp4"/>
<Label Content="Direct Path :" HorizontalAlignment="Left" Margin="10,345,0,0" VerticalAlignment="Top" Width="82"/>
<MediaElement x:Name="Dir_Video1_mp4" Margin="115,278,331,10" Stretch="Fill" Source="C:\Users\user\Desktop\Video1.mp4"/>
<Label Content="Embeded Gif Here ↓:" HorizontalAlignment="Left" Margin="452,34,0,0" VerticalAlignment="Top" Width="114"/>
<Image Margin="452,65,30,189" Source="pack://application:,,,/Medias/1Correctmation.gif" />
</Grid>
What should I do ?
Update:
I tried to check if has error in MedaFailed event , so check it out
Source="pack://siteoforigin:,,,/Medias/CorrectVideo.mp4" error says :
...
Source="pack://application:,,,/Medias/CorrectVideo.mp4" error says :
Source Code :
https://mega.nz/file/B1Zi2S6C#VtCk8xcdabekNIySc9-6C2yMNdV5z-qHoTCUnYl0CXA
is there any alternative for this issue ?
I am developing a Visual Studio extension in C# and I would like to display a (waiting) transparent animated gif inside a tool window panel.
I have googled a lot and I only could find very complex solutions involving special dedicated libraries or some equivalent source code to add to my project. For example, I could find this post: How do I get an animated gif to work in WPF?. But I cannot understand why it would require such complex solutions for such a basic feature as animated gifs.
Can't XAML support natively animated gifs?
On my side I tried the following in my XAML file:
<Image Source="pack://application:,,,/MyPlugin;component/Resources/busy.gif"
Width="16" Height="16"/>
The image loads (although not animated) while Visual Studio is in edition mode. However, the image NEVER shows in the experimental instance.
What did I miss?
For example, I could find this post: How do I get an animated gif to
work in WPF?. But I cannot understand why it would require such
complex solutions for such a basic feature as animated gifs.
There're some easy ways to display .gif in wpf. For me, I use WpfAnimatedGif to do this.Simply install the nuget package and add the corresponding xmlns then the gif can display in wpf or vs extension.
For WpfAnimatedGif:
1.Add xmlns:gif="http://wpfanimatedgif.codeplex.com" to the xx.xaml.
2.<Image gif:ImageBehavior.AnimatedSource="xxx/xxx.gif" />
Then the .gif can display in the vs extension.
The format in my project:
<UserControl x:Class="VSIXProject2.ToolWindow1Control"
...
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="MyToolWindow">
<Grid>
<StackPanel Orientation="Vertical">
...
<Image gif:ImageBehavior.AnimatedSource="pack://application:,,,/VSIXProject2;component/Resources/time.gif" Height="100" Width="200"/>
</StackPanel>
</Grid>
</UserControl>
Note: To display the image when debugging VS extensions, the source format should be pack://application:,,,/xxx;component/Resources/xxx.gif. It's the different behavior between normal wpf and ToolWindows in vsix. More details see this.
I'll try to explain my problem clearly.
I have a working code in WinForms that has a Board (PictureBox) that shows an image thats generated from a list of users controls (win-forms) by the function UserControl.BitmapCopy() for each user control.
This process begins with a blank image (Graphic type), and for each user control I draw it in a specific location with the function BitmapCopy() of the user control.
The result is an image that looks like a real form (with buttons,labels,etc.), but it’s just an image.
Then I show this image in a picture Box.
Now I need to implement this code in WPF, but I can’t generate an image of each user control with BitmapCopy().
I found this code that does it, so now I can generate a bitmap for each user control, but I don’t know what is the best way to create the Big Board that eventually shows a bitmap that has all the user controls images inside it, in different locations.
I would appreciate any help.
This is the equivalent in WPF:
<Window x:Class="MiscSamples.VisualBrush"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VisualBrush" Height="300" Width="300" x:Name="Window">
<StackPanel>
<TextBlock Text="Hi, Im a Window!"/>
<TextBox Text="This is a TextBox"/>
<Slider Width="200"/>
</StackPanel>
<Window.ToolTip>
<ToolTip DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
<Grid Height="400" Width="400">
<Grid.Background>
<VisualBrush Visual="{Binding}"/>
</Grid.Background>
</Grid>
</ToolTip>
</Window.ToolTip>
</Window>
The Window's ToolTip consists of a grid painted with VisualBrush whose Visual is the Window itself. It looks like this:
As you can see, Exactly 0 lines of C# code are required to achieve the same in WPF.
I would like to insert a picture une a button, and, when i run my application, i have a XmlParseException.
I don't know why :(
<Button Grid.Column="2" HorizontalAlignment="Left" Height="20" Margin="15,2,0,0" VerticalAlignment="Top" Width="41">
<Button.Background>
<ImageBrush ImageSource="Images/search.png" Stretch="None"></ImageBrush>
</Button.Background>
</Button>
Anyone have an idea please ?
Make sure that your Visual Studio project contains an Images folder which in turn contains the image file search.png. Moreover the Build Action of the image file must be set to Resource.
You must check if your image has set Build Action property to Resource:
I have a WPF <Image> that I am trying to display on a <Button> inside of a <Toolbar> with the following code.
<ToolBarTray>
<ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" >
<Button Name="StampButton" Click="StampButton_Click">
<Image Source="/MyApp.Resources;component/Resources/MyImage.png"
Height="16" Width="16" Stretch="Fill"/>
</Button>
</ToolBar>
</ToolBarTray>
The image shows up just fine at design time. However, at runtime nothing is displayed. The resources are in another dll called MyApp.Resources. The button is actually created just fine and the click event works fine also.
Set your image build action to "Resource". Try to use full source path.
I was getting same problem - image displaying at design time, but not at runtime.
I had build action set to resource, tried different pack uris etc etc, but solution was simple
<Window.Resources>
<BitmapImage x:Key="your_image_key" UriSource="your_image.png" />
<Image Source="{StaticResource your_image_key}"/>
</Window.Resources>
This is what worked for me, from a site called wpf-tutorial.com:
<Image Source="/WpfTutorialSamples;component/Images/copy.png" />
Images is a subfolder of the project folder in my case, and I assume also in this sample.