Adding Image into Bing map - windows-phone-7

I've tried adding a image via the following however it is still not working. The image type is a content.
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));
//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;
// Center the image around the location specified
//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);
mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);

Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources
This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.
I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.

I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?
Assuming that you have added it directly:
If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:
Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
However if you have set the "Build Action" to "Content" you should use the GetContentStream method
Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;

Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root):
/pages - Contains the actual pages and therefore also the page containing the above code
/images - Contains the actual PNG images that have to be referenced
In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows:
1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level)
2. Now reference /images -> ../images
3. Now add the actual file reference -> ../images/myimage.png
If you use the correct URI the problem is solved.

The main question seems to be how to get true uri.
For me, the following table helps me in this case (I only have it in German):
http://msdn.microsoft.com/de-de/library/aa970069.aspx
example:
// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
example 2:
Uri uri = new Uri("pack://application:,,,/File.xaml");
or Codebehind:
'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example

/projectname;component/images/menu/lost.png
Is the correct way, the rest of your answer is really not working

Related

best way to show images stored in the app from a webview in SWIFT

I have a webview and would like to show an image in the webview (html)
My HTML :
hello !img src="myimage.png" alt="myimage" height="42" width="42"!
(I used ! as tagend and tagstart, because I don't know how to add this here without be interpreted as HTML, even I pasted as code)
The myimage.png should be stored in app itself and not be loaded from a websource.
I don't know how to do that in a best practice way. Any help ?
UPDATE
I tried with referenced Article, but still not succeeded:
My Code for this:
let path:NSString = NSBundle.mainBundle().bundlePath;
var baseURL:NSURL=NSURL(fileURLWithPath: path as String)!;
var htmlString:String! = texttemp
myWebView.loadHTMLString(htmlString, baseURL: baseURL)
The same Image I can already load like the following -> works:
var image = UIImage(named: "myimage.png");
Your updated code isn't right. You are creating a path to the bundle, not to the specific file. You need to use the NSBundle method pathForResource:ofType (or one of its variants) to build a path to your file. Then use that to create the URL.
The pathForResource:ofType family of methods return nil if the file can't be found, so you should check that you are getting back a path.
EDIT:
Looking at it more closely, I see that you are using the URL as the base URL for a call to loadHTMLString. This does look like a sound approach. What is your HTML string, and where is the image in your bundle?

Embedded resource and adding images

I am developing a form application with monodevelop. I'm using gtk#.
I need to add an image to a widget in the form. I am also trying to make this image an embedded resource as well, before including it in the form.
so far this is what I have:
HBox CharacterPic = new HBox();
Image LegionnairePic = new Image('somehow load the embedded resource image here');
CharacterPic.PackStart (LegionnairePic);
In the 'Solution' section to the left, I have added .jpeg files and changed their 'build action' to 'embedded resource'. However, I cannot access/load them onto the form them as so:
Image LegionnairePic = new Image(<namespace>.<resource>);
How do I add the image resource to the form? Am I even adding the resource correctly?
I believe you have to access the embedded files with the following method:
// string resource_id is the Resource ID of the file in the sidebar “Properties”
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_id);
This returns the resource file as a Stream.
If this helped, thank that guy like I did :)

How to upload image on server's image folder

This is my spring project structure.
I have to upload hotels picture in image folder. But i am not getting path of image folder.I tried below code
//There is some code here
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
itemName = item.getName();
File savedFile = new File(request.getRealPath("/")+"image-folder\\"+itemName);
item.write(savedFile);
}
but it is giving error : Directory not exist.
Now I have two question.
What is solution of above problem?
Is this a right way to upload image in server's image folder? Because it could be temporary i have read on stackoverflow?
Edited:
Runing project from eclipse and when i printed path it gives
E:\DeepakWork\current\jmx_examples.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\HotelManagment\image(file-name)
I first want to answer the second most important question
Is this a right way to upload image in server's image folder?
No, it is not. Because it is a security issue! Never ever upload any user generated content in the web application folder!
Instead upload the files in some fixed (configured) folder somewhere outside of the Tomcat Directory.

Why does XAML image source property contain "/MyApp;component"?

I'm developing a Windows Phone app.
I'm using an image and when I select a picture using Properties panel I get the following XAML:
<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>
Why am I getting "/MyApp;component/..."? (Is there any better way?)
If I try to do Image.Source="Assets/Icons/GameImage.png" why does it not work?
This is because your image has it's build action set to Resource (Which is the default). If you switch it to Content you can set the source in your XAML like this:
<Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>
To set it in code you can do this:
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;
You should be using Content for performance reasons. See this article for more detail: http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action
Don't forget to add:
using System.Windows.Media.Imaging;
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;
You can use :
BitmapImage obj = new BitmapImage();
obj.UriSource = new Uri(mera_image.BaseUri,file.Path);
Any content marked as an embedded resource is loaded from an assembly. Thus, usage sites need to know specify the assembly a resource is embedded in. In your case, this is MyApp.

How to use System.Drawing.Image in RDLC Image Control?

Is it possible to use System.Drawing.Image in an RDLC Image Control?
All I have been reading were 3 methods:
database
embeded resource
external file
Thank you thank you.
EDIT:
Following up from this .NET or C# library for CGM (Computer Graphics Metafile) format? I now got the image in System.Drawing.Image format and want to display it as part of the report (as an image) --- that's what I want to do.
Not sure if this is what you are looking for, but if you have an image in code and you want to show it in the report, create a wrapper object that has a property that returns the image as a byte array and give then an instance of this wrapper-class with the valid image to the report as a ReportDataSource.
Something like:
ReportDataSource logoDataSource = new ReportDataSource();
logoDataSource.Name = "LogoDS";
logoDataSource.Value = new List<LogoWrapper>() { yourLogoWrapper };
localReport.DataSources.Add(logoDS);
In the report you then you can the image as it were from the database
=First(Fields!LogoByteArrayProperty.Value, "LogoDS")
The wrapper looks something like:
class LogoWrapper{
...
public byte[] LogoByteArrayProperty{
get{
// Return here the image data
}
}
}
I use this quite often. It has the advantage that I don't have to add the image to the db or add it as a resource of every report. And furthermore, the app can say which image should be used.
Please note, the given image format must be known from the rdlc-engine.
The last question would be, how to convert a system.drawing.image to a byte array. I work with WPF and therefore, I dont known. But I'm sure google will respond to this question very reliable.
You Can use the 'Database' Source Option along with Parameters to Dynamically set Image Source from Byte Arrays.
Code Behind:
var param2 = new ReportParameter()
{
Name = "CompanyLogo",
Values = { Convert.ToBase64String(*ByteArrayImageObject*) }
};
ReportViewer1.LocalReport.SetParameters(param2);
rdlc File:
1- Add Text Parameters 'CompanyLogo' and 'MIMEType'
2- Set the Value Property of the Image to =System.Convert.FromBase64String(Parameters!CompanyLogo.Value)
3- Set MIME Type Property to
=Parameters!MIMEType.Value
4- Use 'Database' As Source
How can I render a PNG image (as a memory stream) onto a .NET ReportViewer report surface
i am not quite sure what do you want to do with this but in general it is not possible.Image Control is just a image holder in the RDLC files.These 3 options specify the location from where the image control takes the image which to display from- database, embeded resource or external file. If you give me more info on what do you want to achieve i can give you some kind of solution.
Best Regards,
Iordan

Resources