Uploading an image on a button click in XMAL - xamarin

Why this does not load the image?
private void OnButtonClickedLoadImage(object sender, EventArgs e)
{
ImageSource imgSrc =
ImageSource.FromFile("C:\\MyApp\\MyPicture.png");
ImageViewerc.Source = imgSrc;
}

If you want to load local images, in Android, Place images in the Resources/drawable directory with Build Action: AndroidResource. In ios, The preferred way to manage and support images since iOS 9 is to use Asset Catalog Image Sets. Then use Asset Catalog Image Sets. The picture name can get the picture.

Thank you all (including Jason) for your help. Based on everyone's comment above, I corrected my code to properly load my image like this:
ImageSource imgSrc = ImageSource.FromResource("MyApp.pic2.png", typeof(ImageResourceExtension).GetTypeInfo().Assembly);
ImageViewerc.Source = imgSrc;
The image must be accessed like this: AppName.ImageFileName.ext (I was missing the AppName
Also I should note that the character case between the actual file name in Solution Explorer and the code-behind MUST MATCH or image won't load.

Related

How to save and view image in window phone 8

I a working on a simple window phone application. In which I have two filed Name and Images.
I want to save this item and want to view all saved data.
Now My query How to select images to save? and How to save images and also get images to view.
I also used PhotoChooserTask but How to save selected image and how to get saved images?
I know about how to save image file in Isolated storage. But how to save selected images and get all data?
Thanks,
Hitesh.
Thanks for your reply. I knew about photoChooserTask. I also save my image file in isolated storage. But I dont know what is the images path to save images path in database and how to display all those images in datagrid. I have a table which have fields like ID, Name and Image path. I dont know what to save in imagepath filed if I saved image in isolated storage and how to display all data in datagrid.I used following code to save data into database. IN below code please correct the image path if I was wrong.
CategoryVO newCategory = new CategoryVO()
{
Name = txtCategoryName.Text,
ImagePath = txtCategoryName.Text.Trim() + ".jpg"
};
Expdb.Category.InsertOnSubmit(newCategory);
Expdb.SubmitChanges();
Using the PhotoChooserTask you can actually launch the photo chooser application and handle the selected image.
If you want to integrate this in your application, create the instance of the PhotoChooserTask and call the Show() method. If you want to handle the user’s selection, register the Completed event which will give you handle of the chosen photo.
var photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += PhotoChooserTaskCompleted;
photoChooserTask.Show();
In the completed event implementation, you can get the chosen image as PhotoResult and set the image to your Image control or can use it in other places.
void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
imageChooser.Source = new BitmapImage(new Uri(e.OriginalFileName));
break;
}
}
Source: http://www.codeproject.com/Articles/350126/How-to-use-the-PhotoChooserTask-to-Launch-the-Medi

How to retrieve an image from WP7 photo library?

I'm developing an application where the user can add photos from windows phone 7 photo library and assign them to a particular view. To do this I save the OriginalFileName on the database (LINQ to SQL). Later I want to recover the photo and load it into the view. Do you know what I can do? currently I have this code but does not work.
When the user has selected the picture I keep his name in the variable fileName:
private void photoChooserTask_Completed (object sender, PhotoResult e)
{
BitmapImage image = new BitmapImage ();
e.OriginalFileName = fileName;
image.SetSource (e.ChosenPhoto);
this.Thumbnail.Source = image;
this.Thumbnail.Stretch = Stretch.UniformToFill;
}
Later, when the user wants to save this setting I save the fileName in database.
This is the code when I load the view that must contain the photo.
imgSource var = new BitmapImage (new Uri (picture.Url, UriKind.Absolute));
item.LeftImage.Source = imgSource;
Where picture.Url contains the filename.
Any idea? I saw on the internet that you can keep the whole image, but give it the best possible.
What you should do is save the picture returned from the PhotoChooserTask in the IsolatedStorage.
You will then be able to load it when needed.
Here is how to Read and Save Images.
For what you need is to get the picture by browsing the MediaLibrary without using PhotoChooserTask, because as you experienced, the file name might not be the same if you use different methods.
For the custom MediaLibrary browsing interface, you could refer to this codeplex project:
https://multiphotochooser.codeplex.com/

Adding Image into Bing map

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

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

Windows Phone 7 Map Control with custom layer in offline mode

Hi WP7 mobile passionate developers!
I'm trying to use the default provided Bing Map control from Windows Phone controls.
Specifically I'm trying to use a custom TileSource to provide a custom made tiled map that will be stored in the project as a folder (Content files) or in isolate storage.
Down I present the custom class I try to use with map tiles/images stored in "map" folder in ZXY storage format as content files.
public class CustomTileSource : Microsoft.Phone.Controls.Maps.TileSource
{
private string uriFormat = #"map/{0}/{1}/{2}.png";
public string UriFormat
{
get { return uriFormat; }
set { uriFormat = value; }
}
public override Uri GetUri(int x, int y, int zoomLevel)
{
var url = string.Format(UriFormat, zoomLevel, x, y);
return new Uri(url, UriKind.Relative);
}
}
Trying to use this is not working and custom tiles are not shown although no error is thrown.
Does anyone tried to use windows phone map control this way?
If that's not the right approach which one is? Any workaround?
Thank you in advance!
Claudiu
Have you set the Build Action on your image(s) to Content?
The Exception is:
This operation is not supported on a relative URI.
at System.Uri.get_AbsoluteUri()
at System.Windows.Media.MultiScaleTileSource.GetTileLayerUrl(IntPtr nativeTarget, Int32 tileLevel, Int32 tileX, Int32 tileY, Int32 uTileImageIndex, IntPtr& fullTileUri, UInt32& fullTileUriLength)
How to get an image from IsolatedStorage, Ressource or MediaLibrary with UriKind.Absolute i not already found out
...maybe you know?
This seems to be quite a FAQ problem - Map Tile Caching for Offline Viewing - shame there isn't an FAQ solution :/
Did you try constructing an absolute file url using the app id as described in this question? Map Tile Caching for Offline Viewing
I tried,
Did you try constructing an absolute file url using the app id as described in this question? Map Tile Caching for Offline Viewing
Map component is not showing me images, but simple
Image.Source = new BitmapImage(new Uri("file:///Applications/Install/0277CC52-888B-4593-A28D-4CFF818E81E7/Install/maps/-1040122162.jpg", UriKind.Absolute));
Is showing image...
You can find a solution in the blog http://invokeit.wordpress.com/2012/06/30/bing-mapcontrol-offline-tiles-solution-wpdev-wp7dev/
In general, you just need
Override GetUri function in TileSource class: return null to let MapLayerTile ignore this tile, and save the tile information to some background worker
In the background worker, loading the tile from anywhere, either isolation storage or network, and then manually add it to a MapLayer control.

Resources