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.
Related
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>
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>
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" />
I created a simple Windows 8 app with a single page and a flipview. My goal is to slide through a set of images. Microsoft suggests to use a context indicator for image galleries with larger amount of images (I have 25 photos).
So for starters: I hope that flipview is the correct tool to my needs?
And how can I add a context indicator to an existing flipview? So far I added the flipview to the xaml file:
<FlipView x:Name="flipView" Grid.Row="1" VerticalAlignment="Top" Background="Black"/>
Also I added some images in C#:
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/gallery/IMG_0001.jpg"));
FlipViewItem flipvw1 = new FlipViewItem();
flipvw1.Background = brush1;
flipView.Items.Add(flipvw1);
etc...
Check this blog post that describes how to implement one:
http://blogs.u2u.be/diederik/post/2012/08/24/A-CXAML-FlipView-Context-Indicator-for-Windows-8.aspx
Also Callisto includes the implementation that you can just grab from NuGet:
https://github.com/timheuer/callisto/blob/master/src/Callisto/Controls/FlipViewIndicator/FlipViewIndicator.cs
I have a WPF C# project that I am working on and I have multiple (25) Image controls arranged in table (5 columns, 5 rows). Each Image control is called "Image[row][column]" (eg:Image15).
Assigning a different source to the control works fine with ony one problem. No matter which control I use (Image11, Image12, Image 55) it affects Image11. No matter which one I try to change I'll end up changing the first one (Image11). This is the source change code:
BitmapImage src3 = new BitmapImage();
src3.BeginInit();
src3.UriSource = new Uri(#"D:\Electricity\CONSUMER_ON.jpg");
src3.EndInit();
Image15.Source = src3;
This does change the image but acts as if I had written "Image11.Source = src3;". Here's the XAML code just in case it might have anything to do with it.
<Image Height="150" HorizontalAlignment="Left" Margin="11,10,0,0" Name="Image11" Stretch="Fill" VerticalAlignment="Top" Width="150" />
... 23 more lines removed ...
<Image Height="150" HorizontalAlignment="Left" Margin="635,634,0,0" Name="Image55" Stretch="Fill" VerticalAlignment="Top" Width="150" />
if you need any other info, please ask. The images are 200x200 and in JPEG format and are displayed correctly (in the wrong place). The Images for the controls are determined and loaded at run-time.
Are you remembering to create a new BitmapImage for each image control, or are you recycling the same object but changing its contents when you move between images? If you set the source of two Image controls to the same ImageSource and then alter that image, both controls will reflect the changes.