I Working on Windows Phone 8 application.
string Image = "/MyData/" + myObject.ImageName + "big.png";
BitmapImage bmp = new BitmapImage(new Uri(Image , UriKind.Relative));
MyImage.Source = bmp;
I have a image in the folder MyData/, i have 2 sets of images like <imagename>big.png,<imagename>small.png.
So here what is happening is i want to check if <imagename>big.png exists in the location or not, if not pick <imagename>small.png.
How to do it ?
EDIT
I solved it myself, here is how.
File.Exists("path to file") here path should be `folderName/filenames` and not `/folderName/filenames`
Thanks for everyone who helped me.
string image = "/MyData/" + myObject.ImageName + "/big.png";
string fileName = Path.GetFileName(image);
if(!string.IsNullOrEmpty(fileName))
{
MessageBox.Show("File Exist -"+fileName);
}
else
{
MessageBox.Show("No File Exist -");
}
BitmapImage bmp = new BitmapImage(new Uri(image , UriKind.Relative));
if(bmp==null)
{
image = "/MyData/" + myObject.ImageName + "/small.png";
bmp = new BitmapImage(new Uri(image , UriKind.Relative));
}
MyImage.Source = bmp;
Related
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
What I have is this and it's working fine:
if (direction.Equals("UR"))
{
UR_Image.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png", Directory.GetCurrentDirectory())));
}
else if (direction.Equals("UL"))
{
UL_Image.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png", Directory.GetCurrentDirectory())));
}
What I wish to do is below, written as pseudo code:
direction + _Image.Source = new BitmapImage(new Uri(
String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png",
Directory.GetCurrentDirectory())));
How can I implement direction + _Image ?
Direction is a string and UL_Image and UR_Image are image views.
Try the following approach.
YourWindow.xaml
<!-- named controls -->
<Image x:Name="ImageOne" />
<Image x:Name="ImageTheOther" />
YourWindow.xaml.cs
// get image control by name
var control = FindName(string.Format("Image{0}", direction)) as Image;
if (control == null)
return;
// set bitmap once
string path = Path.Combine(Environment.CurrentDirectory, "image.png");
var bitmap = new BitmapImage(new Uri(path));
// assign
control.Source = bitmap;
Where direction is enumeration
public enum Direction
{
One,
TheOther
}
I am first creating Bitmap image file and saving it to some temp location. Later using that file to read in BitmapImage object to compare it with other file. Once comparison is done, I want to delete file but then it throws exception that file is being used by another process. How can I delete this file?
Here is my code:
private void btnLogin_Click(object sender, EventArgs e)
{
string strPath = AppDomain.CurrentDomain.BaseDirectory;
GC.Collect();
if (txtLoginImage.Text != "")
{
string strFileName = txtLoginImage.Text.Substring(txtLoginImage.Text.LastIndexOf('\\') + 1);
Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);
Bitmap NewImage = ConvertToGrayScale(MainImg);
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
NewImage.Dispose();
Uri SourceUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
BitmapImage source = new BitmapImage();
source.UriSource = SourceUri;
IrisSystem.Class.BLL.User_BLL ubll = new IrisSystem.Class.BLL.User_BLL();
DataSet dsUserData= ubll.getlist();
bool isMatchFound = false;
if (dsUserData != null && dsUserData.Tables.Count > 0)
{
foreach (DataRow item in dsUserData.Tables[0].Rows)
{
Uri TargetUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Grayscale\\" + item["GrayScaleImgName"]);
BitmapImage Target = new BitmapImage(TargetUri);
if (source.IsEqual(Target))
{
IrisSystem.frmHome frm= new IrisSystem.frmHome();
frm.strFullName = item["FullName"].ToString();
frm.ShowDialog();
Form.ActiveForm.Close();
isMatchFound = true;
break;
}
Target = null;
}
if (!isMatchFound)
MessageBox.Show("Invalid Credential..","Invalid Operation");
}
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
}
else
MessageBox.Show("Please select image", "Login Error");
}
You need to make sure that your Bitmap objects are disposed properly.
You are not disposing the MainImg object. you need to use using {} block to make sure that objects are disposed properly.
Replace This:
Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);
Bitmap NewImage = ConvertToGrayScale(MainImg);
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\"
+ strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
NewImage.Dispose();
With This:
using(Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text))
using(Bitmap NewImage = ConvertToGrayScale(MainImg))
{
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" +
strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
EDIT:
Replace This:
BitmapImage source = new BitmapImage();
source.UriSource = SourceUri;
With This:
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = SourceUri;
source.EndInit();
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);
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);