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.
Related
I'm looking for a way to take an icon from an executable, and create a new executable (using CSharpCodeProvider) that uses this icon.
The first part is easy, I do it using:
Icon icon = Icon.ExtractAssociatedIcon(path);
The problems come when I want to "attach" the icon. I tried using:
compilerParameters.CompilerOptions = #"/win32icon:"
But this solution requires the icon to be written in a file and I'd like to avoid that (one of the reason being it's such a mess to save an icon with more than 16 colors...).
Is it possible to use the System.Drawing.Icon object in memory directly?
EDIT: Just to be clear, I'm trying to obtain a code that can compile C# code into an executable. This executable will have an icon that is only hold in memory (as an Icon object) and not in a file (as a .ico file).
Use the IconLibrary to save Icon
Icon icon = Icon.ExtractAssociatedIcon(#"C:\Windows\System32\notepad.exe");
MultiIcon mIcon = new MultiIcon();
SingleIcon sIcon = mIcon.Add("notepad");
sIcon.CreateFrom(icon.ToBitmap(), IconOutputFormat.Vista);
sIcon.Save(#"c:\notepad.ico");
And use that for CompilerOptions
More information
https://msdn.microsoft.com/en-us/library/2aaxe43f.aspx
Solution :
In case anyone is looking to do the same thing, here is a method that brought the expected result (icon extracting with correct graphic quality).
I use Tsuda Kageyu IconExtractor in order to get the icon I want and convert it to Bitmap with the transparency.
Then I use the IconLibrary suggested by #mohsen to write in icon as a .ico file and then embed it with the CompilerOptions.
Unable to setImage for UIButton.
when set backgroundColor with Image its showing image which I want to set it "backButton.png"
When setImage to UIButton its not showing at all
backButton.SetImage (UIImage.FromFile ("/Images/backButton.png"), UIControlState.Normal);
I want to setImage to UIButton.
// Below is code
UIButton backButton = UIButton.FromType(UIButtonType.Custom);
backButton.Frame = new RectangleF (5, 5, 45, 30);
backButton.BackgroundColor = UIColor.FromPatternImage (UIImage.FromBundle ("/Images/backButton.png"));
backButton.SetImage (UIImage.FromFile ("/Images/backButton.png"), UIControlState.Normal);
backButton.TouchUpInside += (sender, ea) => {
this.NavigationController.PopViewControllerAnimated(true);
};
I don't know much about the Xamarin. But the directory structure presented in xcode project is different than the actual (after app compiles).
All the files and resources resides in same directory called main bundle.
And to access your resources you just ask for it from the main bundle. You don't have to specify the directory (in which resource resides) of you xcode project navigator.
You can read about more directory structure here
It's because when you set images from Visual Studio from property box, it sets the image path like images\image1.png [when the image1.png file is in \Resources\Images folder.
But MAC doesn't support "\" path, you'll need to provide path with "/".
I have come across recently against this very issue. I read that Apple recently enforced a change that all resources must reside within Resources directory when you create the project in Xamarin. You can create Image directory within the Resources directory and have your images there.
Now to read them back it is as simple as - if images are in Resources directory you can just use
UIImage.FromFile ("backButton.png")
But if your images are inside Resources/Images then use simply
UIImage.FromFile ("Images/backButton.png")
Note lack of backslash before the Images directory which now resides inside Resources directory.
This works for me. Sorry i don't recall the link that told me about the change and storage of Resources within the directory only but this fixed all my resource related issues.
In the code you posted:
backButton.BackgroundColor = UIColor.FromPatternImage (UIImage.FromBundle("/Images/backButton.png"));
backButton.SetImage (UIImage.FromFile ("/Images/backButton.png"), UIControlState.Normal);
You use in one line UIImage.FromFile and in the other line UIImage.FromBundle. If I understood you correct the "FromBundle" version works, so try to use this one for the SetImage as well.
I've added an image logo.bmp into my project, but I can't figure out how to assign it to an Image type. I've started by creating the variabale:
public static Image logo { get; set; }
but I can't figure out how to assign my image to it. This is what I have right now:
logo = Image.FromFile("logo.bmp");
But that assumes the image is in the directory of the .exe. What is the correct way to do this?
You can use System.Reflection.Assembly.GetManifestResourceStream to load a resource from a loaded assembly.
First you must make sure that the file in your Visual Studio project is actually being embedded in the assembly itself. Right click on the file and select Properties, then make sure that Build Action is Embedded Resource.
Now you can call Assembly.GetManifestResourceStream from code in that same assembly and specify the resource name with a fully qualified namespace, such as "YourSolution.YourProject.Images.logo.bmp".
The Image class you are using has a method to load from a stream called Image.FromStream().
So here is a code snippet of what you might do:
Image image;
var stream = Asssembly.GetExecutingAssembly().GetManifestResourceStream("MySolution.MyProject.Images.logo.bmp");
image = Image.FromStream(stream);
Just keep in mind that you may need to dispose the stream separately from the image when loaded this way.
The Assembly class is part of the System.Reflection namespace. An alternative for using Assembly.GetExecutingAssembly() is
typeof(YourClassName).Assembly.GetManifestResourceStream("path.to.your.resource")
This way you can use a type to determine from which assembly you would like to access a resource.
I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:
Cursor newCur = new Cursor("Images\\hand2.cur");
window.Cursor = newCur;
So I used this:
string absolute = System.IO.Path.GetFullPath("hand2.cur");
Cursor newCur = new Cursor(absolute);
window.Cursor = newCur;
This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.
But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?
I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.
EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
window.Cursor = new System.Windows.Input.Cursor(info.Stream);
The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}
You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.
var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);
I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:
window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));
C# - Loading image from file resource in different assembly
I have a PNG image file which is stored in a project called SomeProject and displayed various times using XAML in that same project. In a different assembly, I now wish to access that same image. Previously I was simply specifying a relative path to the actual file which worked fine. However, when I build a release installer, the image files are packed into the SomeProject.DLL.
Is there any easy way I can access the PNG file from another assembly without simply resorting to copying the file locally to the second project? I though it might be possible using 'pack://' but I'm not having much luck.
// SomeOtherProject.SomeClass.cs ...
Image logo = new Image();
BitmapImage logoSource = new BitmapImage();
eChamSource.BeginInit();
// Following line works fine is Visual Studio, but obviously not after installation
// logoSource.UriSource = new Uri(#"..\SomeProject\Resources\Images\logo.png", UriKind.Relative);
logoSource.UriSource = new Uri("pack://application:,,,/SomeProject;component/Resources/Images/logo.png");
logoSource.EndInit();
logo.Width = 100; logo.Height = 100;
logo.Source = logoSource;
Any advice would be good.
If the images you wish to use as Content is in another assembly, you must copy them to the main projects directory.
You can use a Build event to do this:
Right click project that contains images -> Properties -> Buil Events -> edit post build to copy images to main project directory.
Then you have to use it as
pack://application:,,,/ContentFile.xaml
(Or)
If you need it in subfolder
pack://application:,,,/Subfolder/ContentFile.xaml
Have a look at this hfor more information http://msdn.microsoft.com/en-us/library/aa970069.aspx
Try to load your other assembly as followed:
Application.LoadComponent(new Uri(#"AnotherAssembly;;;component\AnotherResourceFilePath/logo.png", UriKind.Relative)));
LoadComponent function returns an object. It is up to you to cast it to the appropriate type.