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");
Related
I have a method that opens a FileStream and creates a BitmapImage, by using the StreamSource property.
Somehow, in a single machine, trying to open a big image (6000x4000px) results in the method returning a 1x1px image instead.
First I thought that the image was being loaded from a shared folder on the local network, but It was stored in the downloads folder of the same computer.
I saw that the image was "blocked/locked" by Windows because it was downloaded from an unverified source, so I opened Properties and unlocked it. Trying to load the image again resulted in the same problem.
The image was fully downloaded.
Background information:
The machine with the problem:
Windows 7 SP1.
32 bits.
Net Framework 4.6.2.
4GB of memory, with 2.5GB being used.
My machine (it works as expected):
Windows 10, build 15063.413.
64 bits.
Net Framework 4.7.
8GB of memory, with 6GB being used.
Code:
public static BitmapSource SourceFrom(this string fileSource, int? size = null)
{
using (var stream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
if (size.HasValue)
{
//It's not possible to get the size of image while opening, so get from another place.
var refSize = fileSource.ScaledSize(); //Gets the size of the image.
if (refSize.Height > refSize.Width)
bitmapImage.DecodePixelHeight = size.Value;
else
bitmapImage.DecodePixelWidth = size.Value;
}
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze(); //Just in case you want to load the image in another thread.
return bitmapImage;
}
}
Usage:
var image = "C:\Image.jpg".SourceFrom(); //Without any other parameter.
Question:
Is there any case that my code is not treating properly, at least that explains why I'm getting a 1x1px image instead of the full size one?
Also, why it does not throw an Exception if it can't load the image?
Probable answer:
My code does not handle if the image finished downloading or not, but the image was fully downloaded, so I'm not sure if this is the case. I've found a similar thread about the same problem.
Working code:
using (var stream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
{
using (var memory = new MemoryStream())
{
stream.CopyTo(memory);
memory.Position = 0;
//...
Just replace this part of the code and use the memory variable instead of stream while setting the StreamSource object.
It seems that when the image file is very large, or for some other reason the source stream can't be read immediately, you'll have to copy the source stream to an intermediate MemoryStream, and assign that to the StreamSource property of the BitmapImage:
using (var fileStream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
using (var memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
The BitmapImage creates a default image with 1x1px when decoding of the image fails. You need to register the DecodeFailed event to detect this.
Exception decodeEx = null;
using (var fileStream = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = fileStream;
bitmapImage.DecodeFailed += (_, e) => decodeEx = e.ErrorException;
bitmapImage.EndInit();
if (decodeEx != null)
throw decodeEx;
bitmapImage.Freeze();
return bitmapImage;
}
In my case it turned out to be a OutOfMemoryException. Indeed decoding only failed when the memory usage was high and the native functionwhich is actually called by BitmapImage (using unmanaged memory) was probably unable to allocate enough memory.
i had the same problem, the CacheOption is not in the right place on your code!! juste add it befor the endInit();
source.CacheOption = BitmapCacheOption.OnLoad;
like this ->
BitmapImage source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.StreamSource = fs;
source.EndInit();
How can i get an Image from a base64 string on Windows Phone 8.1?
I know how to decode the image if that helps?
var imageBytes = Convert.FromBase64String(base64StringWithImageData);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, ms.AsRandomAccessStream());
But nowhere have i found how i can get a Image from any of that.
All of the solutions i have found are for wfp or pre win phone 8.1.
I need this to work in a windows store app.
If possible i don't want to save the image to disk first and then load it.
Background: I get Images (customer signatures) from the server in custom objects where images are stored as base64 encoded strings. So the images aren't stores on a website or on disk.
Update
The answer that was suggested doesn't work for me :(
I changed it a little to get it to compile. The code below is from the suggested answer.
var imageBytes = Convert.FromBase64String(_orderEntity.Signature);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
ms.Write(imageBytes, 0, imageBytes.Length);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(ms.AsRandomAccessStream());
signatureImage.Source = bitmapImage;
}
But that code throws a "The component cannot be found. (Exception from HRESULT: 0x88982F50)"
Google sais that means that the image format is unknown :(
This is (one of) the strings i use as input:
"iVBORw0KGgoAAAANSUhEUgAAAYwAAACQCAIAAACUOMtJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGpMAABqTAfnkTrYAABEeSURBVHhe7Z0J0JXTH8d/xVspWkSLmLK1UKORbUojMqVlZI+xJYRRg7xpTGWZIUwhGcxkKWRLiiJjD61ka6hRSZZUKr2RpaT3/z1u/+ut995znufe53nuOef5nmlMo+c+55zP79zv/Z1zfud3alRWVgoLCZAACdhKoKatDWO7SIAESEARoEhxHJAACVhNgCJltXnYOBIgAYoUxwAJkIDVBChSVpuHjSMBEqBIcQyQAAlYTYAiZbV52DgSIAGKFMcACZCA1QQoUlabh40jARKgSHEMkAAJWE2AImW1edg4EiABihTHAAmQgNUEKFJWm4eNIwESoEhxDJAACVhNgCJltXnYOBIgAYoUxwAJkIDVBChSVpuHjSMBEqBIcQyQAAlYTYAiZbV52DgSIAGKFMcACZCA1QQoUlabh40jARKgSHEMkAAJWE2AImW1edg4EiABihTHAAmQgNUEKFJWm4eNIwESoEhxDJAACVhNgCJltXnYOBIgAYoUxwAJkIDVBFIjUj16SMeOUl5utTXYOBIggWoEalRWVnqOpXVrWb58lz5ed52MG+d5r9k9EvCFgO8i1a2bvP9+Dm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vJhYQMO//TllzuVKFoxqt5UVI3bDHEDGLSV8hTpt44vC0WAImXCBS3AGjP22sePjzfiPONtVS27aZmppZH9O/QRU1Ts3+FqaGaDigwrX1QgAYpUMHAZtwXJXiZOlGee8TOLCyZ0CPvCwhNW/eE60XsKNjT4VNwEKFJhCGdXfGbNkjFjVOKXIhekw1Qe17PYqkMaFtzqjD9lZWqCyaXxuFjzvYUQoEgVQk1pE1avsXc2bpw6FIKTaxs2FPSi0n0IC16I6jr7bJXHDnkLqE2lMwVr1hOgSBU3QrK+1ccfqyDJKVNUBIDNBWtM7dqp61uQqQZ/pzbZbCy27V8CFKmIBkJWrRYvlrvvVpEB2I8Lfmg5olbkeE1GlRAH36GD2kDEXThcb4qPNt8cAwGKVAxQscSe0Sy4V7Nnq7jK+fOjjF3SNBkahBh3pHLHxhyyZeHv+D+Z2RwLCbhJgCIVp92y7hUWsF5+WVaskLVrVejm+vXqqAouekBEe2FHVZBkrlUrQSInBDHh7AvSsSM6tGPHnefpMpJU9b9x9pLvJoFYCVCkYsWb/+UZ/coeWMHccNUq5W3B58LRFpySQ9aqOnV2hqrjJi4oDs70ZqZsyBKT9YyySkRfqUSWZLVxE6BIxU2Y7ycBEiiKQCpznBdFjB8mARJIlABFKlHcrIwESCAsAYpUWGJ8ngRIIFECFKlEcbMyEiCBsAQoUmGJ8XkSIIFECVCkEsXNykiABMISoEiFJcbnSYAEEiVAkUoUNysjARIIS4AiFZYYnycBEkiUAEUqUdysjARIICwBilRYYnyeBEggUQIUqURxszISIIGwBChSYYnxeRIggUQJUKQSxc3KSIAEwhKgSIUlxudJgAQSJUCRShQ3KyMBEghLgCIVlhifJwESSJTA/wBcyHVBDaExMgAAAABJRU5ErkJggg=="
I can decompile that string just fine in both a windows Form app:
public static Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
var imageBytes = Convert.FromBase64String(base64String);
var ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
var image = Image.FromStream(ms, true);
return image;
}
and a wpf app:
public static Image Base64ToImage(string base64String)
{
try
{
byte[] binaryData = Convert.FromBase64String(base64String);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();
Image img = new Image();
img.Source = bi;
return img;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return new Image();
}
So why cant i get it to work on Windows Phone??
I found a solution that works!
var imageBytes = Convert.FromBase64String(base64String);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])imageBytes);
writer.StoreAsync().GetResults();
}
var image = new BitmapImage();
image.SetSource(ms);
}
Found the solution here: Load, show, convert image from byte array (database) in Windows Phone 8.1
and there is a the solution here: http://www.codeproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an
I cannot decode images back from their encoded form as a (Jpeg) byte array retrieved from my database to be used as image sources for my WPF application.
The code I am using to encode them as a Jpeg byte array is as follows:
public byte[] bytesFromBitmap(BitmapImage bit)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bit));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
This is taking my Image taken directly from a webpage and assigned to an Image control like so:
var img = new BitmapImage(new Uri(entity.Image.ImageSrc)); //the entity has been saved in my DB, having been parsed from html
pbImage.Source = img;
This works just fine, I encode the BitmapImage and it saves just fine. But when I retrieve it from the DB and try to display it in another window, I cannot get it to work after trying every example I can see online - all either render nothing, or a black box or a visual mess not at all similar to the image I encoded.
Neither of the following have worked for me:
public BitmapSource GetBMImage(byte[] data)
{
using (var ms = new MemoryStream(data))
{
JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource frame = decoder.Frames[0];
return frame;
}
}
public static BitmapImage ImageFromBytes(byte[] imageData)
{
if (imageData == null)
{
return null;
}
else
{
var image = new BitmapImage();
using (var mem = new MemoryStream())
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
} //this throws a 'No imaging component suitable to complete this operation was found' exception
Among other uses of memory streams and decoders I just can't get this to work - can anyone help?
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();
}
Is it possible to set the source of an image in WP7 to a stream? Normally I'd use BitmapImage to do this in Silverlight but I don't see that option in WP7. Here's my code:
var request = WebRequest.CreateHttp("http://10.1.1.1/image.jpg");
request.Credentials = new NetworkCredential("user", "password");
request.BeginGetResponse(result =>
{
var response = request.EndGetResponse(result);
var stream = response.GetResponseStream();
// myImage.Source = ??
}, null);
The reason I ask is because I need to provide credentials to get the image - if there's another way to approach the problem I'm open to suggestions.
Yes, use this code:
var bi = new BitmapImage();
bi.SetSource(stream);
myImage.Source = bi;
In case WritableBitmap you can use:
WriteableBitmap wbmp = new WriteableBitmap(1000, 1000);
Extensions.LoadJpeg(wbmp, stream);
Image img = new Image();
img.Source = wbmp;
Try this one
<Image Name="Img" Stretch="UniformToFill" />
var bitImg= new BitmapImage();
bitImg.SetSource(stream); // stream is Stream type
Img.Source = bitImg;