BitmapImage PixelWidth and PixelHeight property from UriSource - c#

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();
}

Related

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");

C# Removing transparancy from BitmapImage

i'm using a .png as a BitmapImage. The image is being used as a texture on a 3d model.
There is some transparancy in the image which i want to remove.
How do i remove it? (as efficiently as possible, using VisualBrush or System.Drawing.Image is not an option.)
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(fullPath, UriKind.Absolute);
bi.EndInit();
bi.Freeze();
ImageBrush br = new ImageBrush(bi) { Stretch = Stretch.Fill };
DiffuseMaterial mat = new DiffuseMaterial(br);

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;

Unable to display Image created from Base64 BitmapImage

I want to display an Image in a StackPanel from a base64 string but it's not working correctly.
string base64encodedImage = el.Value;
byte[] imageData = Convert.FromBase64String(base64encodedImage);
Image imageSection = new Image();
BitmapImage image = new BitmapImage();
using (MemoryStream memStream = new MemoryStream(imageData))
{
image.BeginInit();
image.StreamSource = memStream;
image.EndInit();
}
imageSection.Source = image;
panel.Children.Add(imageSection);
Example Base64 Image:
iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
The BitmapImage gets a width and height, the Image however does not and nothing is displayed, what am I doing wrong?
Refer to this post to find the correct way to get the BitmapImage from byte array.

Fill WPF rectangle with Image

I created new WPF control, added a Rectangle to it, and everyhing works alright, its drawn like it should be. But I just cant paint the rectangle with an actual Image.
BitmapImage bi = GetImage();
ImageBrush imgBrush= new ImageBrush(bi);
this.rectangle.Fill = imgBrush;
But this code just makes the rectangle transparent, except the stroke.
This is the GetImage() method:
BitmapImage bi;
using (MemoryStream ms = new MemoryStream())
{
bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
texture.SaveAsPng(ms, texture.Width, texture.Height);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
ms.Close();
}
return bi;
texture is an Texture2D class, that is made before this code.
If I return Bitmap insted of BitmapImage here and then save that Bitmap the picture is drawn correctly.
Thank you for your help
This is the correct way to convert Bitmap to BitmapImage:
using(MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
Thanks to "Pawel Lesnikowski", he posted the anwser in the following topic:
Load a WPF BitmapImage from a System.Drawing.Bitmap

Categories

Resources