Set WPF Image Source from a Hyper Link (from Internet) - c#

I try to set WPF image source from an Internet link. How can I do this?
I tried this, but doesn't work:
Image image1 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("link" + textBox2.Text + ".png", UriKind.Relative);
bi3.CacheOption = BitmapCacheOption.OnLoad;
bi3.EndInit();

Prepending "link" to the URL is certainly incorrect. Just make sure that you type the full path of your image into your textbox.
// For example, type the following address into your text box:
textBox2.Text = "http://www.gravatar.com/avatar/ccac9a107581b343e832a2b040278b98?s=128&d=identicon&r=PG";
bi3.UriSource = new Uri(textBox2.Text, UriKind.RelativeOrAbsolute);

Related

Display image from DataGridviewImageColumn in combobox

I ahve a dgv inside a winform and i want to display an image from the dgv image column inside a combo box drop down list.
i can add other values within the dgv to the combobox:
var cbString = dgvLineSymbols.Rows[i].Cells["LINE_NAME"].Value.ToString();
if (cbString != "")
{
cbAllSymbols.Items.Add(dgvLineSymbols.Rows[i].Cells["LINE_NAME"].Value.ToString());
}
if i try adding the value of the image column i get text describing the image as a bitmap.
Ive tried a bunch of things but there isnt a lot of information available on this one.
This was my most hopeful attempt but still did not work, also this required additional dll's being loaded and was causing errors elsewhere in my application.
System.Windows.Controls.Image theImage = new System.Windows.Controls.Image();
BitmapImage bitmap = new BitmapImage();
MemoryStream strm = new MemoryStream();
strm.Write(bytearr, 0, bytearr.Length);
bitmap.BeginInit();
bitmap.StreamSource = strm;
bitmap.EndInit();
theImage.Source = bitmap;
There must be an easier way to achieve this considering how straightforward it is to add an image to a dgv:
if (File.Exists(file))
{
Bitmap img;
img = new Bitmap(#"" + file);
dgvLineSymbols.Columns[1].Width = 100;
dgvLineSymbols.Rows[i].Cells[1].Value = img;
}

How to add an existing item via code in wpf

I am attempting to copy a selected image to a folder and then want to display it with an Image object. The copying works fine, but when I want to display it it seems like the program cannot find it. Displaying the image only works if I manually use "add existing Item". Is there a way to add it automatically?
Here is my code:
string name = "image1";
OpenFileDialog dialog = new OpenFileDialog();
Nullable<bool> dialogOK = dialog.ShowDialog();
if(dialogOK == true)
{
File.Copy(dialog.FileName, #"..\..\Images\" + name + ".png", true);
image.Source = new BitmapImage(new Uri(#"Images\" + name + ".png", UriKind.Relative));
}
("image" is defined in xaml)
It seems safer to use an absolute path for loading the BitmapImage:
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == true)
{
var targetFile = #"..\..\Images\" + name + ".png";
var currentDir = Environment.CurrentDirectory;
var targetPath = Path.Combine(currentDir, targetFile);
var targetDir = Path.GetDirectoryName(targetPath);
Directory.CreateDirectory(targetDir);
File.Copy(dialog.FileName, targetPath, true);
image.Source = new BitmapImage(new Uri(targetPath));
}
In order to release the file directly after loading the BitmapImage, load it from a FileStream:
BitmapImage bitmap = new BitmapImage();
using (var stream = File.OpenRead(targetPath))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
image.Source = bitmap;

WPF Add a png image from resources with C# at runtime

I'm trying to add an image in a stack panel at runtime. My image is in the resources of the application. Here's the code that I have for the moment:
Image image = new Image();
ImageSourceConverter isc = new ImageSourceConverter();
image.Source = isc.ConvertFrom(Properties.Resources.entity16_10) as ImageSource;
image.Height = 16;
image.Width = 16;
panel.Children.Add(image);
I've got a null pointer on the line where I'm trying to use the converter, I don't know if it's the good way of doing this.
Here's how i do it :
object imguri = new Uri("/MyAssembly;Component/MyImageFolder/MyImage.png", UriKind.Relative);
BitmapImage ni = new BitmapImage(imguri);
Image img = new Image();
img.Source = ni;
return img;

Update image file from parallel Task issue

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.

read local image in wp7

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.

Categories