BitmapImage - image downloading issue - c#

I use this code to get images from Internet
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(url, UriKind.Absolute);
image.EndInit();
RSSImage.Source = image;
And sometimes there are no images.
It seems that it happens because of the timeout and etc.
Anyway have I use some async. approaches to get image in time?
Any clue?

Loading image asynchronously (C# 5.0 and .NET Framework 4.5):
using (var client = new WebClient()) {
var bytes = await client.DownloadDataTaskAsync(url);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = new MemoryStream(bytes);
image.EndInit();
RSSImage.Source = image;
}

Related

DataPackageView Bitmap looses transparency

Using the .NET framework, when retrieving the image from the clipboard by converting the IRandomAccessStreamReference, the transparency on .PNG images is lost. How can i prevent this?
var content = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
if (content.Contains(StandardDataFormats.Bitmap))
{
IRandomAccessStreamReference imageReceived = await content.GetBitmapAsync();
BitmapImage bitmap = null;
using (IRandomAccessStreamWithContentType imageStream = await imageReceived.OpenReadAsync())
{
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = imageStream.AsStream();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
}
}

How to display webp format image in WPF? [duplicate]

I'm unable to retrieve an image from a url. Previously I was unable to connect to the site at all until I set HttpClient headers. I'm able to retrieve images from other sources but not this particular one.
Code for retrieving image:
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri("https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp", UriKind.RelativeOrAbsolute);
img.EndInit();
Console.Out.WriteLine();
ImageShoe.Source = img;
If I try to retrieve a different image using a different url, for example https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png it works fine.
Update:
Seems that using a byte array is the way to go but I'm still not sure what is wrong here.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
var url = "https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp";//baseUrl + productUrl;
var result = await client.GetByteArrayAsync(new Uri(
MemoryStream buf = new MemoryStream(result);
var image = new BitmapImage();
image.StreamSource = buf;
this.ImageShoe.Source = image;
WPF does not natively support the WebP image format.
You could simply request a supported format like PNG, by using fmt=png instead of fmt=webp in the request URL:
ImageShoe.Source = new BitmapImage(
new Uri("https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=png"));
If you really need WebP support, the following methods downloads a WebP image and first converts it to a System.Drawing.Bitmap with help of the libwebp wrapper for .NET library. A second conversion then converts from System.Drawing.Bitmap to BitmapImage:
The wrapper library is available via NuGet, but you also have to download the wrapped libwebp library for the desired platform, i.e. x86 or x64, as explained on the wrapper library's home page.
private async Task<BitmapImage> LoadWebP(string url)
{
var httpClient = new HttpClient();
var buffer = await httpClient.GetByteArrayAsync(url);
var decoder = new Imazen.WebP.SimpleDecoder();
var bitmap = decoder.DecodeFromBytes(buffer, buffer.Length);
var bitmapImage = new BitmapImage();
using (var stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
}
return bitmapImage;
}
I've tested it with
ImageShoe.Source = await LoadWebP(
"https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp");

Append image to image.source in wpf

I am trying to append image to image source but after executing the code image is not displaying in my page.
Code:
Bitmap bmp = (Bitmap)data.img;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
this is data.img specifications that I want to append to imhPhoto.Source.
After working around this issue, the solution for this question is:
call the function to get BitmapImage and save it in photo variable like this:
BitmapImage photo = byteToImage(byte[] buffer)
this functionto convert byte to BitmapImage
public BitmapImage byteToImage(byte[] buffer)
{
using(var ms = new MemoryStream(buffer))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}
finally, append converted photo to image source like this:
imgPhoto.Source = photo;
You can assign path like this.
//for App folder path
//var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+"\\Image\\pic.jpg";
//for machine path
var path = #"C:\Users\User1\Pictures\pic.jpg";
Bitmap bmp = new Bitmap(path);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
If this solves your problem :
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(#"g:\screenshot.png");
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;

Outofmemory exception when do bitmapimage dispose

I have created WPF windows application for display more images using grid. My below code getting OutOfMemory Exception when I run my application.exe.
byte[] buffer = File.ReadAllBytes(path);
File.Delete(path);
if (buffer == null)
return null;
using (MemoryStream mStream = new MemoryStream(buffer))
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = mStream;
bi.EndInit();
bitmap = bi;
bitmap.Freeze();
mStream.Close();
mStream.Dispose();
}
I found some solution from stackoverflow and changed my coding as following below,
BitmapImage image = new BitmapImage();
{
image.BeginInit();
// image.CreateOptions = BitmapCreateOptions.n;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
File.Delete(path);
bitmap = image;
image.UriSource = null;
image = null;
}
But this code getting exception as that image used by another process or cant open from locked file.
I am totally confused why my application often caused by OutOfMemory or used by another process exception?
Taken from the comments, you are doing something wrong. You are initializing an obejct ob type BitmapImage and instantly declare it as null. So everything you have declared beforehand has gone down the crapper.
you should leverage the using() statements functionality here. If the code leaves this statement the GarbageCollector will automatically take over and dispose everything for you:
using(BitmapImage image = new BitmapImage())
{
image.BeginInit();
// image.CreateOptions = BitmapCreateOptions.n;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
bitmap = image.Clone();
}

BitmapImage PixelWidth and PixelHeight property from UriSource

I have this:
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
bi.UriSource = new Uri(url, UriKind.RelativeOrAbsolute);
,where url =www.xyz.com/abc.jpg
My question is "How to get the bi.PixelWidth and bi.PixelHeight?" because both are coming as zero. I want to resize big images in to thumbnails.
I have tried this one.
I am fairly new to WP7 development. I cam across this issue and is actually still trying to figure out how to do this.
But I could get it to work with WritableBitmap like so :
Uri uri = new Uri("/image.jpg", UriKind.Relative);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage.UriSource = uri;
WriteableBitmap img = new WriteableBitmap(bitmapImage);
using (MemoryStream ms = new MemoryStream())
{
// write an image into the stream
Extensions.SaveJpeg(img, ms, img.PixelWidth, img.PixelHeight, 0, 100);
byte[] byteArray = ms.ToArray();
}

Categories

Resources