two project in the same solution image url problem - c#

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.

Related

unable to setImage to UIButton in Xamarin 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.

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.

How to display image which is stored on Local drive or network drive?

I have a asp page in which i have to display the image which is stored in my local disk C: and in some cases from network drive
Example path:--C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg
I have done the following code in c# code behind
Image image = new Image();
image.Height = 150;
image.Width = 150;
string path = #"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";//this path will come from database dynamically this is for test only
image.ImageUrl = path;
this.Controls.Add(image);
but image is not showing up so i stumbled upon the this SO Question and i updated my code as below
for page which is showing image
Image image = new Image();
image.Height = 150;
image.Width = 150;
string path = #"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";
image.ImageUrl = "ImageWriter.aspx?path=" + path;
this.Controls.Add(image);
and for proxy page
Response.ContentType = "image/jpeg"; // for JPEG file
string physicalFileName = Request.QueryString["path"];
Response.WriteFile(physicalFileName);
this is working fine but i have two question
1) Why the file is accessible from the physical path while proxy from page but other page cannot able to access it ?
2) And is there any other way to do it ?
Any help would be great thanks.
The images are not available because that path has no relative context to an asp.net site user through a browser who will probably be on another PC and network. To fix this, all you need to do is create an ASHX handler page that fetches the images that are on the server's local drive or network and serve them as an image to the browser:
public void ProcessRequest(HttpContext context)
{
string imgName = context.Request.QueryString["n"];
context.Response.ContentType = "image/png";
string path = #"C:\Users\Public\Pictures\Sample Pictures\" + imgName;
Image image = Image.FromFile(path);
image.Save(context.Response.OutputStream, ImageFormat.Png);
}
And then in your asp just point an image url to the ashx:
image.src = "images.ashx?n=penguin.jpg";
1) Why the proxy page can access the physical path but other page cannot able to access it ?
The other page can. However, the other page isn't accessing the file. It's just setting the file's path to the HTML output for the client (web browser) to access it. Resulting in this:
<img src = "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" />
Since the user doesn't have access to that file, the img tag won't work. The purpose of the "proxy" page is to serve the content of the file from a location which the user can access, that location being the proxy page.
2) And is there any other way to do it ?
Nope. Either the user has to have direct access to the file via some virtual path on the web server, or you have to have a proxy which reads the file's contents and sends it to the user. It has little to do with your C# code and everything to do with what the user can see on the web server. Unless the file is in a web-accessible folder, the user can't see it.
This is a very common and standard approach to securing files behind a web application. Sometimes, for example, you want to check the user's authorization before allowing them to see a file. If the file were publicly accessible, they could just go to it directly. But if there's a proxy page between the user and the file then that page can enforce authorization.
On my experience, the best way, just to avoid all the security problem that finally make impossible the above solutions, is to setup a local website and set the image url like this: image1.ImageUrl="http://pcname/photofolder/myphoto1.png".
pcname is the name of the local web server and the path is the path of a virtual directory set in the local IIS. To setup a local IIS is very simple, google have tons of tips, and to setup a virtual directory is only needed to enter the iis, open the default website, right-clik on "add a new virtual directory" and set the name (photofolder in my example) and make it point to a physical local disk folder and the trick is done. This has also a vantage, that's to say that you can access that folder in all clients connected to the lan.

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.

Categories

Resources