I've just started a very simple WPF application which consists of a main large image and four smaller images.
In order to assist with the layout, I created some JPEGs in MsPaint containing the images -2, -1, 0, +1 and +2 and just copied them into the top level of the project directory.
The XAML segment contains, for the five images:
<Image Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="4" Grid.RowSpan="1"
Margin="0,0,0,0" Name="imgPicture" Stretch="Fill" VerticalAlignment="Top"
Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/zero.jpg" />
<Image Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
Margin="0,0,0,0" Name="imgPicMinus2" Stretch="Fill" VerticalAlignment="Top"
Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/minus2.jpg" />
<Image Grid.Column="2" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
Margin="0,0,0,0" Name="imgPicMinus1" Stretch="Fill" VerticalAlignment="Top"
Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/minus1.jpg" />
<Image Grid.Column="3" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
Margin="0,0,0,0" Name="imgPicPlus1" Stretch="Fill" VerticalAlignment="Top"
Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/plus1.jpg" />
<Image Grid.Column="4" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
Margin="0,0,0,0" Name="imgPicPlus2" Stretch="Fill" VerticalAlignment="Top"
Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/plus2.jpg" />
When I try to set the source property for the plus2 image, it complains with a dialog box stating:
Property value is not valid.
Details
|
V
The file plus2.jpg is not part of the project or
its 'Build Action' property is not set to 'Resource'.
Yet if I rename the file to plus3.jpg or plus2x.jpg, I don't have that problem.
Why is it complaining about plus2.jpg specifically?
Is it possible that you previously added then removed plus2.jpg?
Is it possible that you have not built the solution between the steps?
Clear src, build solution, set src to known working image, build solution, set src to non-working image.
Clear src of all images, build solution, set "Build Action" of all images to "None", build solution, set build-action to "Resource", build solution, set src of all images.
(Most Likely) Possible corrupted image, image with invalid color palette, image saved in invalid format.
See if you can load the image during run-time
Load image with Resouce Editor and "Save As".
The file extension does not make a difference, because the internal graphics format determines the image type. Meaning, if you created a jpg but saved it as bmp, the designer will still load the image correctly.
Try <Image.Source> <BitmapImage UriSource="sampleImages/bananas.jpg" /> </Image.Source>
I tried to duplicate the problem with no luck. I was able to use images that I "added existing" and images where I specified src="file:///c:..." Images inside the project, with and without the build-action set. Even images outside the project.
My other thought is there is something wrong with the image, but I did not add the comment. Yet, now that I think about it, the designer is going to try and load the image once you set src. If the image cannot be loaded then an exception will be thrown. Since you are in design-time, you get the invalid property prompt with an "obsure" details message.
What if you load the image during run-time to see what kind of exception is thrown, if any.
Don't really care about it. VS2008 when creating WPF applications has dozens of such warnings and most of them disappears after build. If app starts and is working - leave it alone. Personally I've installed around 10-15 hotfixes for various WPF related bugs in VS2008. VS2010 is much better.
Besides it's probably better to include image files in resources - you add them to the project (Add existing item) and then check whether the build action is set to Resource. (right click on that file in Solution Explorer and choose Properties)
Related
I've got the following XAML:
<Image Width="100" Height="100" Source="{Binding ImageUrl}" CacheMode="BitmapCache" Grid.Column="0" />
ImageUrl is currently hardcoded to point at an URL.
What's troubling me, is that whenever I start the app from "scratch" (just started the emulator), the images won't load.
If I then rerun the app, the images show up just fine - probably because the image is now available in the cache.
So how do I get the image to show up the first time too ?
Quick summary - i add image control on simple wpf window and set image .while design time it shows and but in run time its doesn't and not showing any exception
<Grid>
<Image Name ="im1" HorizontalAlignment="Left" Height="50" Margin="127,147,0,0"VerticalAlignment="Top" Width="50" Source="pack://siteoforigin:,,,/Resources/chetas logo.bmp" />
</Grid>'
I got it .. when i set my image build in action property as embedded resource
I want to include an image in my project from behind-code, in order to permit me to set the image as fill of my rectangle!
I'm inserting an image in this way:
<Rectangle Grid.Column="2" Grid.Row="2" Margin="4,4,4,4" Style="{DynamicResource rectangle_style}" >
<Rectangle.Fill>
<ImageBrush ImageSource="image.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
For create this background image I must include the image file in a project with right click on file and set "include in a project".
Can you help me to include the image from behind-code in run time, because I will have necessity to set image from most file in a folder!
One way is to do "Include in Project" or "Add existing item" and under Properties, set "Copy Local" to Always.
Note in this case, you need to get the application folder path
<ImageBrush ImageSource="c:\[YourPath]\[YourProject]\someImg.png" Stretch="UniformToFill" />
Or bind to a method that provides the current application path for you. (Do a search for this as there are many such questions answered on SO).
This means when you deploy the app, you have to deploy the image with it (it'll end up in the bin folder with whatever path structure you give it).
Another way is to do the same, and set Build Action to Resource - this will make your application compile with the image inside the DLL. Downside is the DLL will be larger, but you won't have to remember to copy the file when you deploy.
Access the resource by name (no front slash)
<ImageBrush ImageSource="someImg.png" Stretch="UniformToFill" />
Hope it helps!
I have program in which I have lots of buttons. Each of button has background set as
<Button x:Name="mybutton" HorizontalAlignment="Left" Height="30" Margin="76,110,0,0" VerticalAlignment="Top" Width="25" Click="some_click">
<Button.Background>
<ImageBrush ImageSource="Resource/button_picture.png"/>
</Button.Background>
</Button>
Image is showing as background in .xaml when program not running but when i run application image is not there as background of button. How do i debug this background in button ? Is there any goofy error that is there?
Let's make sure we have the following properties set right in your scenario
1) Build Action -> Resource
2) Copy to Output Directory -> Do not copy
3) Instead of using the relative path for image source, try using a full path to the image like this (I say this because I don't know where the image resource is located in your project, using relative path is perfectly normal in WPF)
<Image Source="pack://application:,,,/AssemblyNameContainingImageResource;component/Resource/button_picture.png" />
You need to change the code like this
<Button x:Name="mybutton" HorizontalAlignment="Left" Height="30" Margin="76,110,0,0" VerticalAlignment="Top" Width="25" Click="some_click">
<Image Source="Resource/button_picture.png"/>
</Button>
Change the Build Action of button_picture.png to Resource if it is content. Also check the Copy to output directory property value
Hy!!
I added a png to the debug dic. Now my Xaml Code:
<Button Height="23" HorizontalAlignment="Left" Margin="160,249,0,0" Name="button1" VerticalAlignment="Top" Width="57" ">
<Image Source="\back.png"></Image>
</Button>
The image won'T be found. I read some tutorials about a image in a button, but they all speak about resources etc.
Please write your answer step by step i am very new to WPF
Add the file to the solution. Open the image properties by right click on an image and make sure the build action for image is set to Resource.
You have to add the image to the solution and give the path Like and Set its property BuildAction to Resource
<Image Margin="2" Source="/ApplicationName;component/FolderName/back.png" />