C# Clear a BitmapImage or the instance of it - c#

I would like to clear a bitmap image. I've tried both
uploadImage.Source = null;
and
uploadImage.Source = "";
This is the code I used to make the image:
// BitmapImage.UriSource must be in a BeginInit/EndInit block
BitmapImage myBitmapImage = new BitmapImage();
string curItem = destinationFolder + "\\" + listBox1.SelectedItem.ToString();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(#curItem);
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
uploadImage.Source = myBitmapImage;

uploadImage.Source = null should do it. How are you checking the value of the Source property? I'd suggest Mole.
If you are relying on what you see on screen this may be wrong due to property changes not being notified. Try creating a DependencyProperty for the Image or a regular property raising a notification through the INotifyPropertyChanged interface.

Related

C# WPF MenuItem with image as background programmatically

I'm very bad with XAML, but I just need to create an MenuItem, that has an Image instead of Text.
I already got it working in a way, that the image is presented and loaded out of ressources, but on Mouse Over the image is disappearing.
Could you help me to get this done programmatically? In the internet I only find XAML examples but nothing programatically.
Thanks in advance.
var addOnFrameworkMenuItem = new MenuItem();
var image = new Image();
Assembly asm = Assembly.GetExecutingAssembly();
System.IO.Stream imageStream = asm.GetManifestResourceStream("MyProject.Ressources.image.png");
BitmapFrame bmp = BitmapFrame.Create(imageStream);
image.Source = bmp;
ImageBrush ib = new ImageBrush();
ib.ImageSource = bmp;
addOnFrameworkMenuItem.Width = 150;
addOnFrameworkMenuItem.Height = 31;
addOnFrameworkMenuItem.Background = ib;
Thank you Jim,
your hint helped me. For my problem on disappearing image i just needed to put the image into the header:
addOnFrameworkMenuItem.Header = image;
Additionally I found out to set most of the style properties on following way:
// Create a style to apply to the button
Style s = new Style();
s.TargetType = typeof(System.Windows.Controls.MenuItem);
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontSizeProperty, 11.0));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.BackgroundProperty, Brushes.Orange));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.ForegroundProperty, Brushes.Black));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontFamilyProperty, new FontFamily("Arial")));
s.Setters.Add(new Setter(System.Windows.Controls.MenuItem.FontWeightProperty,FontWeights.Bold));
Trigger t2 = new Trigger();
t2.Property = System.Windows.Controls.MenuItem.IsMouseOverProperty;
t2.Value = true;
t2.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Violet));
s.Triggers.Add(t2);
addOnFrameworkMenuItem.Style = s;

Fast way to check is image exists in assets in Windows Phone

My application for Windows Phone 8.1.
I need to find a way to check image aviability in assets resources in my application.
At first, I had following solution:
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var folder = await package.GetFolderAsync("Assets\\Makes");
var files = await folder.GetFilesAsync();
var result = files.FirstOrDefault(p => p.Name == imageName);
if (result != null)
{
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
Image img = new Image();
BitmapImage bi = new BitmapImage(imageUri);
img.Source = bi;
btn.Content = img;
}
else
{
TextBlock label = new TextBlock();
label.Text = text;
btn.Content = label;
}
It works. But, unfortunately, very very slow.
Anyway, next part of code working even in case if asset is not existing:
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
BitmapImage bi = new BitmapImage(imageUri);
In case if file not existing, the image is empty, but not null.
Is there are any good way, to check, if image created empty from resource?
Or a really fast way to check existing of packaged resource file?
Thank you

Getting NotSupportedException when trying to set image from isolated storage

I'm trying to set image for my tile in the background agent for my application:
ShellTile t = ShellTile.ActiveTiles.First();
if (t != null)
{
var filePath = Path.Combine("Tiles", "test1.jpg");
StandardTileData tile = new StandardTileData();
tile.Title = "Title text here";
tile.BackgroundImage = new Uri(#"isostore:\" + filePath, UriKind.Absolute);
t.Update(tile);
}
but then on t.Update(tile) it throws NotSupportedException :-( Isnt the path ("isostore:\") correct?
new Uri(#"isostore:" + filePath, UriKind.Absolute);
Without the backslash.

C# file access error

I have the following C# code :
string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
Image image = Image.FromFile(selectedFile);
string thumbnail_name = CreateThumbnail(image);
myImage.height = image.Height + "px";
myImage.width = image.Width + "px";
ftp.upload(myImage.internalName, selectedFile, directory);
The process cannot access the file `'D:\..\IMGP2695.JPG'` because it is being used by another process.
I get this error when the code try to run ftp.upload. I know what the error means but i don't know what to do to close the handle to the opened file.
Try to dispose image object by wrapping Image.FromFile with using:
string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
string thumbnail_name = string.empty;
using(Image image = Image.FromFile(selectedFile)){
thumbnail_name = CreateThumbnail(image);
myImage.height = image.Height + "px";
myImage.width = image.Width + "px";
}
ftp.upload(myImage.internalName, selectedFile, directory);
Because you calling Image image = Image.FromFile(selectedFile); before uploading , for some reason.
If you look on Image.FromFile documentation you will see the following string:
The file remains locked until the Image is disposed.
That means, that according to the code provided, you can init Image even after the upload finished.
ftp.upload("NAME_OF_THE_FILE_RECOVERED_FROM_FILE_ITSELF", selectedFile, directory);
Image image = Image.FromFile(selectedFile);

displaying picture from local folder or from net

imagesHello -
I want to display a picture from a local folder in a picturebox, however if that picture fails to load, I woud like to download the image from a website and display it. I have no idea how to do this, but what I have is this:
try
{
pictureBox1.Image = System.Drawing.Image.FromFile("images\\" + filename + "_0001.gif");
XmlIn1.Close();
}
catch
{
string downloadPath = "http://www.website.com/images/" + filename + "_0001.gif";
pictureBox1.Image = System.Drawing.Image.FromFile(downloadPath);
XmlIn1.Close();
}
Why not use the ImageLocation property?
pictureBox1.ImageLocation = "http://skins.gmodules.com/ig/images/logos/approved/beveled_white.png";
Above code will display Google Logo from Web.
try something like
WebClient wc = new WebClient();
MemoryStream ms = new MemoryStream(wc.DownloadData(<imgURL>));
pictureBox1.Image = Image.FromStream(ms);

Categories