unable to setImage to UIButton in Xamarin C# - c#

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.

Related

Difference between #"pack://application:,,,[...] and IO.Directory.GetCurrentDirectory()?

I've noticed that when I add a line of <Window.Background></Window.Background> in the XAML file or in the C# code this.Background = new ImageBrush(new BitmapImage(new uri([...])); if I put in the "Uri" this: new Uri(#"pack://application:,,,/Myapp;component/image.jpg") I get an error when I try to compile it which says: "Could not locate resource "image.jpg" "
But if I change this to new Uri(System.IO.Directory.GetCurrentDirectory()+"\\image.jpg"); it never gives me an error. Why???? What is the difference between both methods? I am very comfortable with Directory but why the compiler doesn't show an error "Locating" the resource with IO.Directory? What's the difference?
The questions may sound too noob, but I don't understand why it gives an error in #"pack://application[...] and not with GetCurrentDirectory()
A pack:// Uri, will search embedded resources for the image. The resources are inside the DLL/EXE, rather than deployed to the same folder. To use an image as a WPF Resource you need to set the Build Action on the image to Resource.
Using the Directory approach simply searches the physical file system for the image.
More info here on WPF Pack Uri's.

Having Trouble Accessing an Embedded Resource [duplicate]

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.

Portability worries because of custom cursor path in C#

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

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.

two project in the same solution image url problem

I have two projects on my solution and I want to use other project pictures dynamically How can I link to other project picture (using imageurl property)
System.Web.UI.WebControls.Image imd = new System.Web.UI.WebControls.Image();
imd.Width = 220;
imd.Height = 215;
imd.ImageUrl = "~/Content/Images/Attachments/test.png;
pnlattachment.Controls.Add(imd);
To access an image from another project, you'd have to embed that image as an embedded resource into the assembly and access it using GetWebResourceUrl: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx
Or, if we are talking two web sites, you could use the ../../ notation to access the appropriate directory, since ~ only works in the context of the current virtual directory.
HTH.

Categories

Resources