My requirement to get image from images folder from my current stimulation explore.
we need access image from Images folder.
My code is gives an error at ImageSource MoleImage = new BitmapImage(new Uri(".Images/Section5Q4femail1.pngeMole")); line
private Image CreateImage()
{
Image Mole = new Image();
Mole.Width = 25;
Mole.Height = 25;
ImageSource MoleImage = new BitmapImage(new Uri(".Images/Section5Q4femail1.pngeMole"));
Mole.Source = MoleImage;
return Mole;
}
Create Uri object like this
new Uri("/Earnest_Individuals;component/Images/Section5Q4femail1.pngeMole");
where Earnest_Individuals is your project assembly name.
Related
I have a picture in the resources that I have been loading using the following code:
var path = new Uri("pack://application:,,,/DocumentHandlingTouch;component/Resources/pdf.jpg", UriKind.Absolute);
My computer crashed in between builds and now it doesn;t load that picture onto the canvas at all any more.
Any Ideas?
Please check if you have set the build action of the image files to 'Resource' in your project.
var pdfimage = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(#"pack://application:,,,/DocumentHandlingTouch;component/Resources/pdf.jpg", UriKind.Absolute);
src.EndInit();
pdfimage.Source = src;
I am trying to add Images to a GridView, but the images never actually show.
Instead, I'm just display the class name in a Thumbnail looking layout.
Here is the code that I have been tweaking
public async void setImage(string url) {
Byte[] imageBytes = await httpClient.GetByteArrayAsync(url);
MemoryStream stream = new MemoryStream(imageBytes);
var randomAccessStream = new MemoryRandomAccessStream(stream);
BitmapImage image = new BitmapImage();
await image.SetSourceAsync(randomAccessStream);
imagesGrid.Items.Add(image);
}
Thanks for the help. I ended up with the following block that works.
public void setImage(string url) {
Image img = new Image();
img.Source = new BitmapImage(new Uri(url));
imagesGrid.Items.Add(img);
}
This is because you're adding BitmapImages to your visual tree. BitmapImages are not visual elements. What you need to do is create an Image and set the Source property to the URI of your image file. There are a number of examples of this in xaml and also code on my blog here.
I haven't tested this but it should be something like this:
Image img = new Image();
img.Source = new BitmapImage(new Uri(url));
imagesGrid.Items.Add(img);
I'm trying to get all images from a directory in WP7. I already use
var temp = Directory.GetFiles(#"\Pictures");
but it doesn't seem to work.
by the way i also tried the isolated storage solution. but none of those solution seems to work.
Is i a wp7 limitation?
you shoud use following code for images from the resources:
Uri uri = new Uri(uriString, UriKind.Relative);
String originalUriString = uri.OriginalString;
Uri resourceStreamUri = originalUriString.StartsWith("/", StringComparison.Ordinal) ? new Uri(originalUriString.TrimStart('/'), UriKind.Relative) : uri;
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(resourceStreamUri);
if (null != streamResourceInfo)
{
stream = streamResourceInfo.Stream;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
Image image = new Image();
image.Source = bitmapImage;
}
Probably, you should use IsolatedStorageFile.GetFileNames instead.
Good tutorial: How to: Find Existing Files and Directories in Isolated Storage
You probalby have your pictures as Resource and not as Content. Right click on images in your solution and change the Build Action to Content.
BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
WriteableBitmap wbm = new WriteableBitmap(img);
I get a Runtime error at the line above: "Object reference not set to an instance of an object."
The reason you get the null reference exception is that BitmapImage.CreateOptions Property default value is BitmapCreateOptions.DelayCreation. You can set it to BitmapCreateOptions.None and create WriteableBitmap after image loaded:
BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += (s, e) =>
{
WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s);
};
If the build action of your image file is set to resource, then the following code will work.
Uri uri = new Uri("/ProjectName;component/Images/image.jpg", UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
BitmapImage img = new BitmapImage();
img.SetSource(resourceInfo.Stream);
WriteableBitmap wbm = new WriteableBitmap(img);
Notice that the resource is accessed by the static method GetResourceStream defined by the application class.
Now if you change the build action of the file to Content rather than Resource, you can simplify the Uri sintax considerably.
Uri uri = new Uri("Images/image.jpg", UriKind.Relative);
The difference, in case you are wondering...
If you navigate to the Bin/Debug directory of a Visual Studio project and find the XAP file that contains your program, rename it to a ZIP extension. And look inside.
In both cases the bitmap is obviusly stored somewhere within the XAP file.
With a Build Action of Resource for the file, it is stored inside the DLL file along with the compiled program.
With a Build Action of Content, the file is stored external to the dll file but within the XAP file, and when you rename the XAP file to a ZIP file, you can see the it.
I need to set image source dynamically, please note my image is in somewhere on the Network, here is my code
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(#"pack://application:,,,\\myserver\\folder1\\Customer Data\\sample.png");
logo.EndInit(); // Getting the exception here
ImageViewer1.Source = logo;
Exception:
The URI prefix is not recognized
None of the above solutions worked for me. But this did:
myImage.Source = new BitmapImage(new Uri(#"/Images/foo.png", UriKind.Relative));
You just need one line:
ImageViewer1.Source = new BitmapImage(new Uri(#"\myserver\folder1\Customer Data\sample.png"));
The pack syntax you are using here is for an image that is contained as a Resource within your application, not for a loose file in the file system.
You simply want to pass the actual path to the UriSource:
logo.UriSource = new Uri(#"\\myserver\folder1\Customer Data\sample.png");
None of the methods worked for me as i needed to pull the image from a folder instead of adding it to the application.
The below code worked:
TestImage.Source = GetImage("/Content/Images/test.png")
private static BitmapImage GetImage(string imageUri)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri, UriKind.RelativeOrAbsolute);
bitmapImage.EndInit();
return bitmapImage;
}
You are all wrong!
Why? Because all you need is this code to work:
(image View) / C# Img is : your Image box
Keep this as is, without change ("ms-appx:///) this is code not your app name
Images is your folder in your project you can change it.
dog.png is your file in your folder, as well as i do my folder 'Images' and file 'dog.png'
So the uri is :"ms-appx:///Images/dog.png"
and my code :
private void Button_Click(object sender, RoutedEventArgs e)
{
img.Source = new BitmapImage(new Uri("ms-appx:///Images/dog.png"));
}