Way to use WPF Image without attaching image at runtime - wpf

I know how to attach an image to a XAML <Image> via VB.net runtime code. How would I do this the normal old way. (Attach the image within the IDE without using any code)
Before WPF one would browse for the image (in the Visual Studio IDE) it would somehow place it in the project and it would work.
Now I do this same thing, I can see the image in my Visual Studio IDE but during runtime the image appears blank.
How would I use the VS IDE to correctly browse for the image so it will display at runtime? Perhaps im doing something wrong a bit of help or any guidance would be awesome

I suppose you have a folder in your project which contains your image files, in that case something similar to <Image Source="/WpfApplication1/component;ImagesFolder/ImageName.png" /> would do the trick.
If you're looking for a non-code way, when you click on tag, you can browse for imported files from Source Property from Properties panel

Related

Set default xaml window background image in dll

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

Making icon for a Windows Form: How to make it show up correctly?

OK, so I have built a Windows Form application. I now want an icon for it. So I use the Icon Editor built into Visual Studio 2012. Draw it all out to look nice and purdy. Once I am done, I have a .ico file and I make it the default icon for the project, and also the icon for the one WinForm in the application.
Unfortunately, it does not show up as I have created it! It is displayed as the default icon file as it existed before I modified it in the icon editor. It's a 32x32 4 bit icon. If I change the extension to .bmp it shows up as the default.
It looks like the VS icon editor is editing something else, not the appearance of the icon. Can someone tell me what I am doing wrong?
I used to have a progam called IconArt that would create icons that looked like icons when I used them in VS. IconArt is now abandonware and won't run on my 64bit Windows workstation.
Since I didn't get any answers within the time I was hoping, I posted this question also in the MSDN Visual Studio forum, and got a good answer that I thought I should post here. Credit to Reed Copsey, Jr, for the answer!
This is it:
You'll need to put your design in all of the different versions. ICO
files contain multiple versions of the same image, for different
screen resolutions.
My personal preference is to not use VS - there's an ICO plugin for
Paint.Net (all free) which allows you to make a single image
(typically 256x256), and save multiple versions within an ICO file in
one shot. It's very useful for building icons.
See
http://forums.getpaint.net/index.php?/topic/927-icon-cursor-and-animated-cursor-format-v37-may-2010/
for the plugin.
Since I am a Paint.Net user, the plugin sounded like a great idea, and I tried it. Bingo! This works very nicely.

Why does the Image element give me a design-time error with "add as link"ed images?

I need to display an image, which I've done without problems before, but today I decided to be tricky and use "add as link" instead. Well, now I get:
The file Images/hello.png is not part of the project or its 'Build Action' property is not set to 'Resource'.
Wait... its Build Action is set to Resource. I've seen a Silverlight solution that involves the usage of Merged Dictionaries to share files between Silverlight and WPF projects, but it's not clear to me that this would even apply to my WPF + Image issue.
Has anyone solved this problem before? I could make copies of all of the images, but that seems a little silly if I have a shared repository with clip art and the like.
Dave,
I've just tried to add image as a link to plain WPF application. Build action is "Resource" (don't confuse with "Embedded Resource"). I've added it to the root, and refer to it as <Image Source="/file_name.jpg"/> - all works fine.
The message you have is it compile or runtime? If it's a runtime, how do you refer to the image? Do you see it in Reflector, when you open your assembly (it should be under Resources folder)?
I have images in one assembly which I want to share into another. I've used Add as Link in my second assembly. In the project where the actual image files are located they are in a Resources\Images folder. In the project which links to those files the links are also in a Resources\Images folder. At runtime a XamlParseException claiming "cannot locate resource" is thrown.
My xaml which is referencing the image is in a UserControls folder.
In the project which actually contains the images the path ..\Resources\Images\Blah.png works fine as expected.
Opening the DLLs in Reflector makes it obvious that in the assembly with the linked images holds the images at the root level - the compiler is not respecting the folder location. So in the project with the linked files I have to use ..\Blah.png to find the resource.
Surely a bug in the compiler?

Why can't I set the Image Source in WPF app?

I'm just learning WPF. I'm using VS 2008. I've added an Image control to my XAML, and I added a GIF file to the project. If I use the XAML Designer's property panel to set the image's Source property, the drop-down list for the Source property contains the following:
C:\Sample Project;component/pinHorizontal.gif
There are several problems with this:
My project, named "Sample Project," is not in the root of my drive.
Why is ";component" in there?
If I select this value as given, I get the error "Property value is not valid" (yeah, no kidding).
If I go into the XAML source and set the Source property manually, like so:
<Image Name="PinImage" Source="pinHorizontal.gif"/>
The XAML Designer gives me this error:
"The file pinHorizontal.gif is not part of the project or its 'Build Action' property is not set to 'Resource'."
Why is this task so difficult? How do I assign an image source?
Have a read through this article - though be warned it's not particularly light reading :-)
I suspect what you want for your image path is pack://application:,,,/pinHorizontal.gif. if your image is set to a BuildAction of Resource, this will work fine.
Right click on the image file(pinHorizontal.gif) in the SolutionExplorer, Go to properties, here is the 'Build Action' property, you need to set it to 'Resource'. Hope it helps!!
This post is an "and also"... not an answer to the original question.
I just thought I'd document it in case some other poor booger has this problem in future... and googling that error message finds this SO question... so here's a good place to do so.
I've been creating new icons (actually bitmaps) and adding them "on the fly" to my Visual Studio 2008 project (i.e. Add ~ Existing item). The IDE has been reporting file ${filename} is not part of the project or its 'Build Action' property is not set to 'Resource' errors on my XAML page... but my project builds and runs OK... so what gives???
I tried everything I could think of the get visual studio to refresh it's view of the filesystem, to no avail.
Well, I've just dicovered that restarting Visual Studio "refeshes" its cache of filesystem/build-output contents, which makes those annoying non-errors in the IDE go away. So I add a batch of icons, mark them all as Resources ~ Copy if newer, build, and then restart the IDE, and it's all good.
Cheers all. Keith.
In the Solution Explorer, select your project and right click rebuild the solution, and try to add the image again, it should work fine.
--or--
under the Build tab select rebuild the solution, and try to add the image again.
You can use this piece of code in the XAML to load an image from the absolute path without setting nothing. Use DecodePixelWidth or DecodePixelHeight to save application memory.
<Image>
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\image.png" />
</Image.Source>
</Image>
Hope it helps!
I was having the exact same problem. I just manually typed in the location of the file when the Choose image box pops up and that worked for me.
I opened the Choose image box again after I got it to work and noticed that file:/// was now in front of the file location. I know next to nothing about Visual Basic (which is what I'm using) so there may be a good explanation for this, but I don't have it.

Using Visual Studio Image Library PNGs in a Windows Form app

My question is about the Visual Studio Image Library that comes with VS2008. In the _Common Elements\Annotations folder, there are PNGs with multiple sizes and I was wondering what the intended use of these is. Is there an standard way to implement these images, e.g. in a Windows Forms status bar?
Here are three of the PNGs as an example:
The suggested usage is to basically copy/paste the sized image you want into a new image file. Then simply use that as an icon or image in your controls as you see fit. All of the PNGs already have a transparent background, so it should be really easy to copy/paste.

Resources