Images shows in designer but when ran as debug in vs2013 it gives error:
also if exe ran directly with image in same folder.
information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'.
The error is bcoz it cant find the image. also in xaml view when hover the image sorce it says:
project file expected in c:\user\bsienn\docs\vs2013\project\wpf1\wpf1\image1.jpg
though the pic is indeed in that path and is available.
I want to add an image as background for form, I don't want to add the image in resource, bcoz i want the image to be changed when needed. I have placed the image with exe file also tried to place it in bin/debug and main application folder (wpf1/image1.jpg andalso wpf1/wpf1/image1.jpg).
here is the xaml code, please guide
<Window.Background>
<ImageBrush ImageSource="image1.jpg"/>
</Window.Background>
App structure:
app.exe
image1.jpg
Desired outcome, form with background image
This will do as desired
XAML:
<Window.Background>
<ImageBrush x:Name="MainFormBgrImg"/>
</Window.Background>
code behind:
BitmapImage bitimg = new BitmapImage();
bitimg.BeginInit();
bitimg.UriSource = new Uri(#""+AppDomain.CurrentDomain.BaseDirectory+"backgroundImg.jpg", UriKind.RelativeOrAbsolute);
bitimg.EndInit();
MainFormBgrImg.ImageSource = bitimg;
AppDomain.CurrentDomain.BaseDirectory:
Returns current working directory from where where app ran from, i.e c:\users\admin\Desktop\
Putting image in output folder won't make it available to your XAML.
You need to add image in your project and set it's Build Action to Resource.
Right click on added image in project -> Open Properties -> Set Build Action to Resource.
Related
I am binding to 5 different images in my program and wish to leave the capability of the user to replace or update the photos under the same name. So when updating these pictures, the binding will be notified and change while the program is running.
The program is a Digital VMB (visual management board) at my workplace, so it needs to remain running and have these photos be updated on the server without a hitch. Currently I am binding to a property in my C# which is a string containing the image location.
In c#:
public string OpenFilePathProp { get; set; }
In XAML:
<Image x:Name="OpenWOImage" Source="{Binding OpenFilePathProp}" Stretch="Fill" Margin="25,0,25,0"/>
When the user goes to copy and replace the image with the newer one, they can't as it's "currently open in another process". Which I suppose is the data binding in my WPF.
Can this be overcome by opening the images into a filestream and then binding to the stream? If so, I'm completely unsure on how to bind to a filestream; I'm quite new to WPF AND C#.
Thanks for the help. I HAVE tried to look for a solution to this, but I think I'm just getting confused and I don't think it will resolve my problem since it seems the binding is what's "keeping the image open" and I'm not sure how to bind to an Image object AND close that object to allow for overwriting
EDIT: Thought I should mention that I've managed to copy the images in question to the AppData folder for my VMB program,
like,
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\";
System.IO.File.Copy(Properties.Settings.Default.OpenWOFilePath, System.IO.Path.Combine(AppData, "ROAMaintenanceVMB\\OpenWOFilePath_Copy.jpeg"), true);
this way I can "check" every so often to see that someone has overwritten the photos on the server, and THEORETICALLY copy the "new" photos to the AppData folder, overwriting the previous versions. THIS is where the issue of the images already being open in another program arises.
Following Peter Duniho suggestion about using WriteableBitmap worked in my instance.
string imagePath = "";
Uri imageUri = new Uri(imagePath);
BitmapImage bitmapImage = new BitmapImage(imageUri);
ImageProperty = new WriteableBitmap(bitmapImage);
<Image Source="{Binding ImageProperty}"/>
What's going on here is:
BitmapImage is created from the given path
WriteableBitmap is created based on the BitmapImage (a "deep copy"?)
The important thing here is the original image is not locked, as the binding is tied to the copy of the image of WriteableBitmap - not to the image itself, so the image can be deleted / replaced freely.
I've looked at about a dozen different solutions to get this to work and I just can't seem to execute it properly. I have a file cup.png that I placed in a subfolder called /Images/ located in my project folder under "/Visual Studio 2013\Projects\PointOfSale\PointOfSale\Images\cup.png".
The IDE acts like it can find the file and it shows up in the designer view and everything, and I can compile, but the minute I run it I get a XamlParseException error:
A first chance exception of type
'System.Windows.Markup.XamlParseException' occurred in
PresentationFramework.dll Additional information: 'Provide value on
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an
exception.' Line number '7' and line position '14'.
What in the world am I missing here? I've tried all different forms of these to try and get this to work:
<ImageBrush ImageSource="pack://application:,,,/PointOfSale;component/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/Images/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/PointOfSale;component/Images/cup.png" Opacity="0.1"/>
You have a couple of options (your response to my comment did not answer this directly)
You can include the images as a resource using the BuildAction of Resource.
You can include the images as a piece of content using the BuildAction of Content.
If you choose the first option, then replacing the image would require the entire assembly/executable to be replaced. You can reference the image as "assembly;component/Resources/Images/cup.png" (Note that you need a forward slash at the start of the string).
If you choose the second option, then replacing the image would require you to merely replace the image file. You can reference the image as "/Resources/Images/darkaurora.png" (Note that you need a forward slash at the start of the string).
This question already has answers here:
WPF - Import image as resource
(3 answers)
Closed 7 years ago.
I'm using VS2013. .NetFramework 3 . I'm writing a simple project like as image gallery.
I'm trying to add an JPEG image into Image Control. First I'm adding the test.jpg image as resource. Then I'm adding an Image Control on to Window. In the next step I'm selecting the image in "Source" property. The image displaying into design mode. All is ok. But when I'm running the project nothing displaying.
I searched in google and youtube. I founded some solutions but I haven't solution for my problem
(Sorry for ENG)
Here the code line
<Image Source="pack://siteoforigin:,,,/Resources/1.JPG" ... />
In order to simply display the image in WPF app, do the following:
1). In XAML, add:
<Image Name="imgName" Source="/ProjAssemblyName;component/RelativePathToImageFile" />
2). In Visual Studio IDE, select the image file and check its properties:
Build Action: Resources
Copy to Output Directory: Do Not Copy
Note: in most typical case the project Assembly name is the same as Default Namespace; check it in application property dialog. Also useful info pertinent to your case is included in the Microsoft reference (as pointed out by member #Clemens): https://msdn.microsoft.com/en-us/library/aa970069%28v=vs.110%29.aspx#Resource_File_Pack_URIs___Local_Assembly.).
Another option is to set the image Build Action property to EmbeddedResource and get the BitmapImage object corresponding to the image file programmatically like the following:
BitmapImage _bmpImage = new BitmapImage();
_bmpImage.BeginInit();
_bmpImage.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream(String.Concat(ProjAssemblyName, ImgFile));
_bmpImage.EndInit();
This technique is useful if further image processing is considered.
Hope this may help. Kind regards,
I understand that this question has been asked (and answered) before. However, none of the solutions are working for me.
Below is a screen capture of all the relevant pieces of the puzzle:
Screen capture http://dinosaur-island.com/PlantPictureBoxScreenCap.jpg
As you can see there are numerous bitmaps of plants loaded as resources into the Images folder. There is a form with a picturebox named "PlantPicture". There is string, which I know has a good path (because I've checked it in the debugger):
PicPath = PicPath+".bmp";
Screen capture http://dinosaur-island.com/PlantDebugger.jpg
I've tried numerous ways of loading, casting, etc., etc.
The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being back slashes.)
And then:
pictureBox1.Image = Image.FromFile(#"Images\a.bmp");
I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".
Ok...so first you need to import the image into your project.
1) Select the PictureBox in the Form Design View
2) Open PictureBox Tasks
(it's the little arrow printed to right on the edge of the PictureBox)
3) Click on "Choose image..."
4) Select the second option "Project resource file:"
(this option will create a folder called "Resources" which you can access with Properties.Resources)
5) Click on "Import..." and select your image from your computer
(now a copy of the image will be saved in "Resources" folder created at step 4)
6) Click on "OK"
Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the PictureBox:
pictureBox1.Image = Properties.Resources.MyImage;
Note:
MyImage represent the name of the image...
After typing "Properties.Resources.", all imported image files are displayed...
It depends on your file path. For me, the current directory was [project]\bin\Debug, so I had to move to the parent folder twice.
Image image = Image.FromFile(#"..\..\Pictures\"+text+".png");
this.pictureBox1.Image = image;
To find your current directory, you can make a dummy label called label2 and write this:
this.label2.Text = System.IO.Directory.GetCurrentDirectory();
The accepted answer has major drawback!
If you loaded your image that way your PictureBox will lock the image,so if you try to do any future operations on that image,you will get error message image used in another application!
This article show solution in VB
and This is C# implementation
FileStream fs = new System.IO.FileStream(#"Images\a.bmp", FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
Setting "Copy to Output Directory" to "Copy always" or "Copy if newer" may help for you.
Your PicPath is a relative path that is converted into an absolute path at some time while loading the image.
Most probably you will see that there are no images on the specified location if you use Path.GetFullPath(PicPath) in Debug.
I have a game application in Visual Studio 2012 C#. I have all the .png images I am using in the Resources file of the project.
Have you any idea why I can access all the files but one by using Properties.Resources?
I checked the full filePath and it's set to the resources folder. And it's added in the program as I did Add -> Existing Item and added the image.
It looks just like the other images. I have no idea why it's not loading. I need this since I need to send a .exe by email to my lecturer and without this image the project is nothing!
I added this in the resource file
internal static System.Drawing.Bitmap grid_fw {
get
{
object obj = ResourceManager.GetObject("grid.fw", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
and although now grid is available, it is returning null :/
Found from: Properties.Resources the icon name does not appear in the intellisense
You also need to add the icon to the Resources.resx file. Open it in
Visual Studio and drag your icon into the Icons menu of the resx and
it will become available.
Also, see Adding and Editing Resources (Visual C#)
You can get a reference to the image the following way:
Image myImage = Resources.yourImage;
If you want to make a copy of the image, you'll need to do the following:
Bitmap bmp = new Bitmap(Resources.yourImage);
Don't forget to dispose of bmp when you're done with it. If you don't know the name of the resource image at compile-time, you can use a resource manager:
ResourceManager rm = Resources.ResourceManager;
Bitmap yourImage = (Bitmap)rm.GetObject("yourImage");
The benefit of the ResourceManager is that you can use it where Resources.myImage would normally be out of scope, or where you want to dynamically access resources. Additionally, this works for sounds, config files, etc.