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;
Related
I want to save am image from [URL : https://www.example.com/folders/file.jpg][1]
in an object of BitmapImage
I tried the following :
BitmapImage b = new BitmapImage();
b.SetSource(new Uri("https:www.example.com/folders/file.jpg", UriKind.RelativeOrAbsolute));
But its not working. Why?
This code test and work for me:
Image MyImage = new Image();
// Create source.
BitmapImage bImage = new BitmapImage();
bImage.UriSource = new Uri(#"https:www.example.com/folders/file.jpg", UriKind.RelativeOrAbsolute);
// Set the image source.
MyImage.Source = bImage;
Tell me if work for you or need more info. Good luck man!
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.
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 get image URL at runtime from the xml file reader. This image URL is pass to the below method to download it dynamically.
public void Display_Image(string MyURL)
{
BitmapImage bi = new BitmapImage();
bi.UriSource = new Uri(this.BaseUri, MyURL);
Img_Poster.Source = bi;
}
But this doesn't work. I don't get any image source. The above code works fine with static URL provided at compile time. What do I need to do more?
The method I suggested below is obsolete. However, creating a new Bitmap image created dynamically with a Uri determined at runtime IS supported and working on the RTM build of Windows 8. Display_Image(url) should work as you expect.
You can get the image stream using the CreateFromUri helper: http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.streamreference.createfromuri.aspx#Y0
var stream = RandomAccessStreamReference.CreateFromUri(new Uri(imageUrl))
You should then be able to set the source of your bitmap to the RandomAccessStream that the helper returns
I've had similar problems with previously-working Bitmap code not working on Windows RT, an early attempt convinces me that it refuses to download anything unless it is going to be displayed on the UI (here, I needed to insert a 1ms delay before assigning sources just to get it to trigger the image download):
var image = .... // reference to animage on the UI
var placeholder = ... // a placeholder BitmapImage
var source = ... // uri to download
image.Source = placeholder;
var src = new BitmapImage(new Uri(source));
src.ImageOpened += (s, e) =>
{
var bi = s as BitmapImage;
image.Source = bi;
};
image.Source = src;
// Delay required to trigger download
await Task.Delay(1);
image.Source = placeholder;
Here's another solution I've tried with success:
var image = .... // reference to animage on the UI
var source = ... // uri to download
var placeholder = ... // a placeholder BitmapImage
image.Source = placeholder;
var bytes = await new HttpClient().GetByteArrayAsync(source);
var img = new BitmapImage();
await img.SetSourceAsync(bytes.AsBuffer().AsStream().AsRandomAccessStream());
image.Source = img;
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"));
}