C# WPF ImageControl not showing selected image [duplicate] - c#

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,

Related

wpf use resource image in xaml [duplicate]

This question already has answers here:
In my WPF application, my loaded PNG logo in image shows at design time but not at run time
(4 answers)
Closed 7 years ago.
How to use resource image in WPF image source.I given like below,
<Image Source="pack://siteoforigin:,,,/Resources/008.jpg"/>.
But it displays in edit page.If i run the application it not displaying in the window.
Please help me to solve this.
Thanks.
you need to provide the correct URI to the resource, and the URI depends on the method you used to add the resource this post give the details you need
How to Embed resources
You can add your image to your project Properties.Resources.Images and then use them like that:
<Image Source="{x:Static Properties:Resources.YourImageName}"/>
EDIT >>>>
I've forgotten to say that you have to add this line at the top:
<xmlns:Properties="clr-namespace:yournamespace.Properties"/>

Set Window background as image from drive on runtime

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.

WPF OpenDirectoryDialog in order to get and set the path of a ListView itemSource

I have a ListView that contains all images of the .jpg format from a given directory. It's filled by the codebehind using the following code:
DirectoryInfo folder = new DirectoryInfo(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + App.putanjaSlika);
FileInfo[] images = folder.GetFiles("*.jpg");
for (int i = 0; i<images.Length; ++i)
{
FileInfo img = images[i];
Thumbnails.Items.Add(new BitmapImage(new Uri(img.FullName)));
}
This works marvelously, but as you can see, the path is pretty much set in stone. What I need is some sort of a BrowseDirectoryDialog that can allow me to "open" a given directory, and use the located path as the argument for the first line of the above code.
I've found this question Open directory dialog and I like the Ookii.Dialogs solution, but perhaps even more the solution under the second answer. However, seeing as how that thread is 3.5 years old, I wanted to ask if a better solution came out, since I wasn't able to find any.
I published this folder browser dialog for WPF in 2011:
http://wpffolderbrowser.codeplex.com/
It is based on an MSDN example and full source code is provided, so you can adapt it as needed. I like it better than the solution mentioned in the linked post, because it displays the "new style" open folder dialog with Vista / Windows 7 look and feel, as opposed to the WinForms dialog which seems to be the same as in Windows 95.
Unfortunately there is no dialogs of WPF. So you have to use Winforms.

Properties.Resources not loading an image

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.

custom cursor in metro app

I am developing a paint like application. I want to change cursor at some instance. So, how can I use the custom cursor in metro app ?
I have found this
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, uint id);
In above method, there is one enum for "Custom" cursor and the second argument is for resource ID. So how can I get that ?
The basic route to doing this:
Create your custom cursor and package it in a .res using a C++ Metro DLL
Take a note of your resource id by peeking into the resource.h file in the C++ project
In my project the resource number was 101 and I didn't adjust.
Add the .res to a CSharp XAML Metro project
Open your .csproj using a text editor
Inside the first property group add a section that points to your .res file
Switch out the cursor to the custom cursor using the function call you referenced, and the resource number you found by peeking at resource.h.
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, 101);
I realize this is a lot. I posted a detailed step by step walk through on my blog at http://blogs.msdn.com/b/devfish/archive/2012/08/02/customcursors-in-windows-8-csharp-metro-applications.aspx . Hope this helps.

Categories

Resources