I have follow C# code (.Net 4.5)
var bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 64;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(_filename);
bi.EndInit();
bi.Freeze();
return bi;
At the opening Local file
C:\Users\KVV\Desktop\1111111111111\Windows%20XP6.jpg
Сatch exception
System.IO.FileNotFoundException
with FileName = C:\Users\KVV\Desktop\1111111111111\Windows XP6.jpg
%20 was replaced space
How can I create working URL for such files (need only local files).
Or may be can create BitmapImage without create url for local file.
Problem may be resolve with FileStream.
using (var stream = System.IO.File.OpenRead(s))
{
var img1 = new BitmapImage();
img1.BeginInit();
img1.CacheOption = BitmapCacheOption.OnLoad;
img1.StreamSource = stream;
img1.EndInit();
im1.Source = img1;
}
But this behavior Uri is similar to bug.
Related
I am trying to update an image by setting the source property every second, this works however causes a flicker when updated.
CurrentAlbumArt = new BitmapImage();
CurrentAlbumArt.BeginInit();
CurrentAlbumArt.UriSource = new Uri((currentDevice as AUDIO).AlbumArt);
CurrentAlbumArt.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
CurrentAlbumArt.EndInit();
If I don't set IgnoreImageCache, the image does not update thus no flickering either.
Is there a way around this caveat?
Cheers.
The following code snippet downloads the whole image buffer before setting the Image's Source property to a new BitmapImage. This should eliminate any flicker.
var webClient = new WebClient();
var url = ((currentDevice as AUDIO).AlbumArt;
var bitmap = new BitmapImage();
using (var stream = new MemoryStream(webClient.DownloadData(url)))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
image.Source = bitmap;
If the download takes some time, it would make sense to run it in a separate thread. You would then have to take care for proper cross-thread access by also calling Freeze on the BitmapImage and assigning Source in the Dispatcher.
var bitmap = new BitmapImage();
using (var stream = new MemoryStream(webClient.DownloadData(url)))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
bitmap.Freeze();
image.Dispatcher.Invoke((Action)(() => image.Source = bitmap));
I'm displaying an image from a base 64 string that came from an API. The problem is, the image is not being displayed.
Here's the code:
profilePictureImg.Source = GetUserImage(user.MobileNumber);
private BitmapImage GetUserImage(string phoneNumber)
{
BitmapImage bitmapImage = new BitmapImage();
var baseAddress = "http://192.168.0.103/vchatapi/api/Images/" + phoneNumber;
var http = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new System.Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "GET";
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
var y ="";
var x = y.FromJson(content);
byte[] binaryData = Convert.FromBase64String(x);
using (MemoryStream ms = new MemoryStream(binaryData, 0, binaryData.Length))
{
ms.Write(binaryData, 0, binaryData.Length);
bitmapImage.StreamSource = ms;
}
return bitmapImage;
}
Any Ideas?? Thanks!
EDIT:
Got the fix. For some reason, it requires to call BeginInit and EndInit.
The image may be decoded as shown in this answer:
var binaryData = Convert.FromBase64String(x);
var bitmapImage = new BitmapImage();
using (var stream = new MemoryStream(binaryData))
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
}
The reason why you have to use BeginInit and EndInit is explained in the Remarks section of the BitmapImage MSDN documentation:
BitmapImage implements the ISupportInitialize interface to optimize
initialization on multiple properties. Property changes can only occur
during object initialization. Call BeginInit to signal that
initialization has begun and EndInit to signal that initialization has
completed. After initialization, property changes are ignored.
This may be one of those cases where it pays not to dispose the stream too eagerly; also, the Write here is unnecessary: you already added the data via the constructor. So just:
bitmapImage.StreamSource = new MemoryStream(binaryData);
return bitmapImage;
does that work?
You can try the following
byte[] binaryData = Convert.FromBase64String(x);
using (MemoryStream ms = new MemoryStream(binaryData))
{
bitmapImage = (Bitmap)Image.FromStream(ms);
}
I use this code to populate WPF Image Control.
string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height;
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = marquee1.Height;
image.Width = marquee1.Height * ratio;
lstItems.Items.Add(image);
Also I have some parallel Task to update this image file.
But when I try to delete it I am getting the error: File is busy by other process you cannot delete this file.
How to resolve this issue?
Thank you!
UPDATES
So thank you all of you!
The final solution needs to implement
src.CacheOption = BitmapCacheOption.OnLoad;
src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
the working code looks like
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad;
src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height;
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = marquee1.Height;
image.Width = marquee1.Height * ratio;
lstItems.Items.Add(image);
result = image.Width;
MSND BitmapImage.CacheOption says:
Set the CacheOption to BitmapCacheOption.OnLoad if you wish to close a
stream used to create the BitmapImage.
Set the BitmapImage.CacheOption to OnLoad:
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
Set the CacheOption property of the BitmapImage to BitmapCacheOption.OnLoad. This will cause it to load the image into memory and close the original file, which should allow you to delete it.
Also I have some parallel Task to update this image file ... when I try to delete it I am getting the error
In general, you can't delete a Windows file while a process is using it. The consumer Windows FAQs include this page to describe this restriction. And as MSDN describes, File.Delete will refuse to delete a file that's in use.
In my app i need to open a local image for cropping. I tried this by setting the path of the image as source to a BitmapImage but it raising an error. How i can read and assign that local image to BitmapImage in WP Mango.
private WriteableBitmap ReadLocalImage(string Uri)
{
StreamResourceInfo sri = null;
Uri uri = new Uri(Uri, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
return wb;
}
use this method for reading local image.
I am using visual studio 2010, (desktop application) and using LINQ to SQL to save image/video or audio files to database in dataType VarBinary (MAX). This I can do... Problem is, I can't get them and display them in xaml because I can't get the converting part correct. Here is what I have so far (though its not working);
private void bt_Click (object sender, RoutedEventArgs e)
{
databaseDataContext context = new databaseDataContext();
var imageValue = from s in context.Images
where s.imageID == 2
select s.imageFile;
value = imageValue.Single().ToString();
//convert to string and taking down to next method to get converted in image
}
public string value { get; set; }
public object ImageSource //taking from http://stackoverflow.com/
{
get
{
BitmapImage image = new BitmapImage();
try
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(value, UriKind.Absolute);
image.EndInit();
Grid.Children.Add(image);
}
catch { return DependencyProperty.UnsetValue; } return image;
}
}
I not even sure if I am on the correct track? And I am assuming that video or audio is quite similar methods?
Since your image is stored in binary format in the database, you want to "stream" this into an image object by leveraging the MemoryStream object.
Looking at your code, your solution will look something like this:
BitmapImage bmpImage = new BitmapImage();
MemoryStream msImageStream = new MemoryStream();
msImageStream.Write(value, 0, value.Length);
bmpCardImage.BeginInit();
bmpCardImage.StreamSource = new MemoryStream(msImageStream.ToArray());
bmpCardImage.EndInit();
image.Source = bmpCardImage;
It's very easy, if you have a binary data and want to create an Image object, use this code:
public Image BinaryToImage(byte[] binaryData)
{
MemoryStream ms = new MemoryStream(binaryData);
Image img = Image.FromStream(ms);
return img;
}
If you already have the bytes, to verify that what you saved is correct you can save the bytes to a file and open it....
string tempFile = Path.GetTempFileName();
MemoryStream ms = new MemoryStream(bytes); //bytes that was read from the db
//Here I assume that you're reading a png image, you can put any extension you like is a file name
FileStream stream = new FileStream(tempFile + ".png", FileMode.Create);
ms.WriteTo(stream);
ms.Close();
stream.Close();
//And here we open the file with the default program
Process.Start(tempFile + ".png");
And later you can use the answer of Dillie-O and stream....
Dillie-O's code makes for a very nice extension method:
// from http://stackoverflow.com/questions/5623264/how-to-convert-varbinary-into-image-or-video-when-retrieved-from-database-in-c:
public static BitmapImage ToImage(this Binary b)
{
if (b == null)
return null;
var binary = b.ToArray();
var image = new BitmapImage();
var ms = new MemoryStream();
ms.Write(binary, 0, binary.Length);
image.BeginInit();
image.StreamSource = new MemoryStream(ms.ToArray());
image.EndInit();
return image;
}