ImageButton in WPF - wpf

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" />

Related

Image control not showing the image when run code

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

Background Image of Button not showing in WPF

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

Label does not change content

I have WPF app with label MainWindow.xaml:
<Label Grid.Row="1" Margin="0,17,0,16" Name="lblLoadDriver" VerticalAlignment="Center" HorizontalAlignment="Left" Height="10" Width="10"></Label>
In MainWindow.xaml.cs there is code, after button click:
lblLoadDriver.Content = ""+DriverInstance.GetType().ToString();
When i debug, I put the mouse over the value of lblLoadDriver browse under "base" section and see content is AS-I-NEED.. i see the text ok, but it didnt change the text on GUI.
Why is this ? Do i need to refresh gui or something ?
thanks.
Perhaps because of Width="10"?
Also Height="10" is a strange idea.

ImageButton in Silverlight 3?

For our .NET project this year we have to create a website in Silverlight.
Now I have been getting along nicely utilizing the grid, making gridrows and colums, but I have got to the part where I have to create navigation buttons.
So I decided to go for the following look of the buttons:
Now, when I add a button to my project and I give it the background of that image, this happens.
I have looked at other methods to create an image button but those involve putting an image INSIDE the button using a stackpanel. I tried that and it looks really bad.
This is the code the button generated:
<Button Content="Button" Height="40" Name="button1" Width="125">
<Button.Background>
<ImageBrush ImageSource="/OndernemersAward;component/Images/button.png" Stretch="Fill" />
</Button.Background>
</Button>
I don't know what's going on here and I hope somebody can help me.
Thanks, Thomas
Putting an image inside the button is the best way to go. You can just have an image inside a button, or you could put a StackPanel inside and arrange other content as you see fit. You can also get rid of the border and background brushes on the button so that it is just your image and nothing else. Try this code:
<Button Height="40" Name="button1" Width="125" BorderBrush="{x:Null}" Background="{x:Null}" IsHitTestVisible="False">
<Image Source = "/OndernemersAward;component/Images/button.png"/>
</Button>
That will make a button that is just your image with no other borders, or colors. You can tweak the other settings as you see fit. Note that in order to use the image as the button, you have to delete the Content tag, since the inside of the <Button> takes the place of it.
I ultimately solved this by using blent. I created an image and there is a control there that can transform the image to a button.

Bizarre problem with WPF XAML file

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)

Resources