Image saved to "LocalFolder" not picked by Notification Toast? - c#

I have saved (Write) an image to "LocalFolder" not being picked up by the notification Toast.
StorageFolder systemLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
string path = systemLocalFolder.Path + "\\" + R.GetResourceString("CachedImageFolder") +
"\\" + R.GetResourceString("CachedImagePrefix") + contact + ".png";
path = path.Replace(#"\", #"/");
path = #"file:///" + path;
image.SetAttribute("src", path);
binding.AppendChild(image);
After setting this image , the toast does not show up.
However doing this:
image.SetAttribute("#Assets/Logo.png", path);
Does show up the toast with the image.
But i want to write a file and then use it and not pick from App Package.
Win8+XAML+C#

The file:/// protocol isn't supported for Windows 8 Store apps BUT you don't need it anyway. Something like this should get you there:
path = #"ms-appdata:///local/" + R.GetResourceString("CachedImageFolder") + "/"
+ R.GetResourceString("CachedImagePrefix") + contact + ".png";

Related

capturing an image from the camera

I am working on a winforms application, the application will attempt to capture an image and save it to the hard drive without the need to present the result or the live stream to the form.
I was using AForge and suddenly I started getting null in my image variable, I tried several other packages and they are all giving the same problem.
I want to know if there is a setting on my system or in my code that is preventing the image from being copied to the variable.
the last one I tried was Microsoft Media Capturing package and the code as follows.
System.Drawing.Imaging.ImageFormat format = null;
format = System.Drawing.Imaging.ImageFormat.Jpeg;
StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
string name = InitialSetup.currentLkr.Name /*+ #"\" + InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg"*/;
name = name.Replace(#"/", "_");
name = name.Replace(" ", "_");
name = name.Replace(":", "-");
name = #"D:\" + name;
StorageFolder sf = await ApplicationData.Current.LocalFolder.CreateFolderAsync(name,CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(sf, InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
//img.Save(name);
//webcam.Stop();
return name+ InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg";

Value of a variable is not updating

I am currently working on a project that sweeps a mailbox for attachments and when one is found it is placed in the user's directory. My problem is that when I check if the file exist in the path, I alter the attachment's name and add a counter and time stamp, that way it is not over written. However, when it goes into the condition and changes the file name it never updates the path variable to include the right value of the Clean name variable.
string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);
string path = employeeStarPath + "\\" + cleanName;
// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{
cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.
}
Now I am aware I can update the path var by calling path = employeeStarPath + "\\" + cleanName; again but I feel that this makes my code a bit confusing.
I might not understood your question but can you just call the line "string path = employeeStarPath + "\" + cleanName;" at the end instead before the if?
string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);
// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{
cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.
}
string path = employeeStarPath + "\\" + cleanName;

Editing image file name in SQL and deleting actual image if there is modification

I have function in my app to edit the user details in that there is feature which allows us to edit the image in the picturebox.
What I am trying to achieve is if user choose to edit the image to another the app should delete the current image and use the new image and save to db. If users edit other details without touching picturebox then no changes should be made.
I have written below code but but it's not working can correct me or suggest better solution for what I am trying to achieve.
string picPath
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "Image files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
profilePicPictureBox.ImageLocation = FileName;
}
if (File.Exists(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg"))
{
File.Delete(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
picPath = (#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
profilePicPictureBox.Image.Save(picPath);
}
when i try to execute the above i am getting a exception on relative path

How to work with ImageButton in code behind in C#

protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string path = Server.MapPath("~//Images//" + FileName);
FileUpload1.SaveAs(path);
string imagepathsource = path;
string imagepathdest = #"D:\\" + Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName;
File.Move(imagepathsource, imagepathdest);
uploadedimage.ImageUrl = "D://" + Session["brandname"].ToString() + "//" + Seasonfolders.SelectedItem.Text + "//" + stylefolders.SelectedItem.Text + "//Images//" + FileName;
}
}
uploadedimage is a ImageButton where I need to display the image using imageurl with that link. The image is not displaying and no error.. I could see when the above code gets execute the page is getting refreshed? What code can be added in this ??
ImageUrl needs an URL.
So instead of assigning it with a local file name on disk, use something like this (where ~ stands for 'application root folder':
uploadedimage.ImageUrl = "~/Images/yourImage.png"
You have to supply it with:
An image inside your application root folder;
An URL that is catched by a HttpHandler, that generates / loads the image from a different location (a little harder to do, but if you need to load from another location then inside application root, this is the best option).

How to save data from form in a specific drive

I want to save my text file in a F drive but this file is written to a default folder of program . How to save it by guiding a path
string[] contents = new string[2];
contents[0] = "Name: " + textBox1.Text;
contents[1] = "age: " + textBox2.Text;
string path = #"F:\\"; // path to file
System.IO.File.WriteAllLines(textBox1.Text + ".txt", contents);
It would be a good idea to actually use your path variable:
string path = System.IO.Path.Combine(#"F:\", textBox1.Text + ".txt");
System.IO.File.WriteAllLines(path, contents);
Because you defining a path,but you don't use it.
string path = #"F:\" + textBox1.Text + ".txt";
File.WriteAllLines(path, contents);
As an alternative, you can use File.Move after you created it like;
File.WriteAllLines(textBox1.Text + ".txt", contents);
File.Move(Directory.GetCurrentDirectory() + textBox1.Text + ".txt",
path + textBox1.Text + ".txt");

Categories