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);
Related
I have an Android-App that takes pictures and saves them in an external storage ("DCIM/Cameras"). But the pictures appear only after restarting my handy.
Is there some kind of Update or a way around this?
My source-code from saving my image:
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
var pictures = dir.AbsolutePath + "/Camera";
string name = System.DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + ".jpg";
string filePath = System.IO.Path.Combine(pictures, name);
FileStream output;
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
try
{
output = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, output);
output.Close();
//Static Class that contains an methode for MediaScannerConnection.ScanFile
MediaGalleryHelper.AddFileToGallery(name);
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
You have to let the photo gallery know that a new photo has been added. You can use below code for that:
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + filePath)));
Where 'this' is an activity.
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
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;
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);
Imagename = objUser.UserID + filename;
Imagepath = "D:\\Shop\\ShopMonkey\\Images" + Imagename;
FileUpload.SaveAs(Path.Combine(#"D:\ShopMonkey_Web_21-6-12\ShopMonkey\Images", Imagename));
objUser.UploadImagePath = Imagepath;
objUser.UploadImagename = Imagename;
System.Drawing.Image img1 = System.Drawing.Image.FromFile(Imagepath);
System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero);
ThumbNailPath = "D:\\ShopMonkey_Web_21-6-12\\ShopMonkey\\ThumbNails" + Imagename;
bmp1.Save(Path.Combine(#"D:\ShopMonkey_Web_21-6-12\ShopMonkey\ThumbNails", Imagename));
objUser.UploadThumbnailPath = ThumbNailPath;
How to delete the image and thumbnail in other function? (Is it necessary to close that first or not?)
I'm guessing that you're trying to delete a file on disk if it already exists for this user upon asp.net upload of an image by that same user again.
This method will delete both image and thumbnail if they exist.
Keep your image creation activities separate from your image clean up activities to keep your intent clear and your code maintainable.
// replace with an entry loaded from a config file
const string ImageRoot = #"D:\ShopMonkey_Web_21-6-12\ShopMonkey";
// replace this is your user instance
object user = new object();
string Imagename = objUser.UserID + filename;
string uploadImagePath = Path.Combine(ImageRoot, "Images", Imagename);
string thumbnailPath = Path.Combine(ImageRoot, "ThumbNails", Imagename);
objUser.UploadImagePath = uploadImagePath;
objUser.UploadImagename = Imagename;
objUser.UploadThumbnailPath = thumbnailPath;
// delete both if they exist
if (File.Exists(uploadImagePath))
File.Delete(uploadImagePath);
if (File.Exists(thumbnailPath))
File.Delete(thumbnailPath);
// replace this with your uploaded file details
object fileInfo = new object();
using (System.Drawing.Image img1 = System.Drawing.Image.FromFile(uploadImagePath)) {
img1.Save(uploadImagePath);
using (System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero)) {
bmp1.Save(thumbnailPath);
}
FileUpload.SaveAs(uploadImagePath);
}