How to display image in WinForms and then delete? - c#

When my form opens it uses the file path that is provided from another form and displays the images, and when I close the form I want it to delete all the images.
But when I close it keeps stating the file is being used, is there a better way of doing it please?
public frmMark(string strImagePath)
{
InitializeComponent();
pbImage1.Image = new Bitmap(strImagePath + "1.jpg");
pbImage2.Image = new Bitmap(strImagePath + "2.jpg");
pbImage3.Image = new Bitmap(strImagePath + "3.jpg");
}
private void frmMark_FormClosing(object sender, FormClosingEventArgs e)
{
File.Delete(deletepath + "1.jpg");
}

Change this:
pbImage1.Image = new Bitmap(strImagePath + "1.jpg");
to this:
pbImage1.ImageLocation = Path.Combine(strImagePath, "1.jpg");
and for the others and your issue should go away because the files will not be locked, so you don't have to worry about unlocking them.

Perhaps create a language extension which clones the original image, load the image then deletes the physical file.
public static class PictureBoxExtensions
{
public static void LoadClone(this PictureBox pictureBox, string fileName)
{
var imageOriginal = Image.FromFile(fileName);
var imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);
Graphics gr = Graphics.FromImage(imageClone);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
gr.Dispose();
imageOriginal.Dispose();
pictureBox.Image = imageClone; // assign the clone to picture box
File.Delete(fileName); // remove original
}
}
Or if not always needing to remove an image add another parameter,in this case remove where the default is to remove which of course you can set the default to false.
public static class PictureBoxExtensions
{
public static void LoadClone(this PictureBox pictureBox, string fileName, bool remove = true)
{
var imageOriginal = Image.FromFile(fileName);
var imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);
Graphics gr = Graphics.FromImage(imageClone);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
gr.Dispose();
imageOriginal.Dispose();
pictureBox.Image = imageClone; // assign the clone to picture box
if (remove)
{
File.Delete(fileName); // remove original
}
}
}
Usage
public frmMark(string strImagePath)
{
InitializeComponent();
pbImage1.LoadClone(strImagePath + "1.jpg");
pbImage2.LoadClone(strImagePath + "2.jpg");
pbImage2.LoadClone(strImagePath + "3.jpg");
}

Related

image doesn't change - WPF

I'm trying to change an image on runtime but it's not working.
I have a user control that when you click on imagebtn it's opening a new window with a list of images.
the next step is to take the image selected from the list, close the new window, and put the image on the imagebtn.
the user control still opens in the background.
this is my code.
NewWindow:
private string myData;
public ImagesWindow()
{
InitializeComponent();
InitialImageList();
}
private async void InitialImageList()
{
//add try catch
string get = await HttpRequests.GetRequest(URLImages);
allJsonCategory = JsonConvert.DeserializeObject<List<ImageArray>>(get);
Console.WriteLine(get);
ImageBoxList.ItemsSource = images;
foreach (var item in allJsonCategory)
{
images.Add(item);
}
}
private void ImageBoxList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedImage = (ImageArray)ImageBoxList.SelectedItem;
myData = selectedImage.full_path;
Console.WriteLine("you clicked on: " + selectedImage.name);
ProductsCategory pro = new ProductsCategory();
pro.imagePath = myData;
this.Close();
}
my usercontrol(in mainWindow):
public void setImage(string imagePath)
{
if (!imageURL.Equals(""))
{
Image imageBtn = new Image();
var imgUrl = new Uri(imagePath);
var imageData = new WebClient().DownloadData(imgUrl);
// or you can download it Async won't block your UI
// var imageData = await new WebClient().DownloadDataTaskAsync(imgUrl);
var bitmapImage = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageData);
bitmapImage.EndInit();
imageBtn.Source = bitmapImage;
//this.imageBtn.InvalidateVisual();
}
}
XAML of the image:
where is my mistake?
thank you all :)

How to display 2 images at same time in winform

In winforms I have two picture box one for portrait and other for Landscape.
Do to the size of the file or some reasons they are not downloading at same time,
let say if portrait image downloaded first,Now I click the updated button it was showing portrait image,
after second image downloaded when I click the update button it was showing Landscape image only.
I need both images should show, after downloading both images, but in my case it showing only one image(the latest downloaded image).
what should I do, Here is the code.
private void DisplayLogos(LogoHeader imageHeader)
{
if (imageHeader.carId == 2)
{
PortraitLabel.Text = "Portrait Image";
PortraitLabel.Visible = true;
MemoryStream ms = new MemoryStream(imageHeader.Images.First());
Image image = Image.FromStream(ms);
Bitmap bmp = new Bitmap(image);
PortraitPictureBox.Image = image;
PortraitPictureBox.Visible = true;
}
else if (imageHeader.carId == 1)
{
LandscapeLabel.Text = "Landscape Image ";
LandscapeLabel.Visible = true;
MemoryStream ms = new MemoryStream(imageHeader.Images.First());
LandscapePictureBox.Image = Image.FromStream(ms);
LandscapePictureBox.Visible = true;
}
}
public class LogoHeader
{
public LogoHeader(Access au, int Id)
{
carId = Id;
}
public int carId { get; set; }
public byte[] image{ get; set; }
public List<byte[]> Images
{
get
{
List<byte[]> logos = new List<byte[]>();
logos.Add(image);
return logos;
}
}
}
In order to keep the image you can't close (or change) the stream, you can simply create another one.
private void DisplayLogos(LogoHeader imageHeader)
{
if (imageHeader.carId == 2)
{
PortraitLabel.Text = "Portrait Image";
PortraitLabel.Visible = true;
MemoryStream ms1 = new MemoryStream(imageHeader.Images.First());
Image image = Image.FromStream(ms1);
Bitmap bmp = new Bitmap(image);
PortraitPictureBox.Image = image;
PortraitPictureBox.Visible = true;
}
else if (imageHeader.carId == 1)
{
LandscapeLabel.Text = "Landscape Image ";
LandscapeLabel.Visible = true;
MemoryStream ms2 = new MemoryStream(imageHeader.Images.First());
LandscapePictureBox.Image = Image.FromStream(ms2);
LandscapePictureBox.Visible = true;
}
}

How do I get an image to save after rotation at runtime?

I am working with WPF and I have an application that the user loads an image file into a RichTextBox and they can rotate the image and print it. I am not sure as to why the image after it has been rotated will not print as it is displayed on the screen. Instead it prints the original. I am new to this so any help would be greatly appreciated!
The following is the code for my application. Code when the retrieve file Button is clicked:
private void retrieve_button_Click(object sender, RoutedEventArgs e)
{
//Retrieve the file or image you are looking for
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";
var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
try
{
System.Windows.Controls.RichTextBox myRTB = new System.Windows.Controls.RichTextBox();
{
Run myRun = new Run();
System.Windows.Controls.Image MyImage = new System.Windows.Controls.Image();
MyImage.Source = new BitmapImage(new Uri(of.FileName, UriKind.RelativeOrAbsolute));
InlineUIContainer MyUI = new InlineUIContainer();
MyUI.Child = MyImage;
rotateright_button.IsEnabled = true;
print_button.IsEnabled = true;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(myRun);
paragraph.Inlines.Add(MyUI);
FlowDocument document = new FlowDocument(paragraph);
richTextBox.Document = document;
}
}
catch (ArgumentException)
{
System.Windows.Forms.MessageBox.Show("Invalid File");
}
}
}
When the rotate right button is clicked the following code is executed:
RotateTransform cwRotateTransform;
private void rotateright_button_Click(object sender, RoutedEventArgs e)
{
richTextBox.LayoutTransform = cwRotateTransform;
if (cwRotateTransform == null)
{
cwRotateTransform = new RotateTransform();
}
if (cwRotateTransform.Angle == 360)
{
cwRotateTransform.Angle = 0;
}
else
{
cwRotateTransform.Angle += 90;
}
}
After the Image has been loaded and rotated the user can use the following code to print:
private void InvokePrint(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
FlowDocument flowDocument = new FlowDocument();
flowDocument = richTextBox.Document;
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(65);
IDocumentPaginatorSource iDocPag = flowDocument;
printDialog.PrintDocument(iDocPag.DocumentPaginator, "Print Document");
}
}
Try this (substitute yourImageControl in the first line, specify which RotateFlipType you want and be sure to reference the System.Drawing dll):
System.Drawing.Bitmap bitmap = BitmapSourceToBitmap((BitmapSource)YourImageControl.Source);
bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
Another option for conversion...
P.S. You would get a better answer in less time if you posted some code and told us more about what you have tried.

C# - Making a BackgroundImage update in real time while editing it in other programs

first i have made an desktop AIR app with drag and drop functions to view the tiles image, but because of some issues, i've tryed to make the same in C#. what do you think is the better choice ? ( i know for this little programm performance is not the question, but i mean the dufficulty and complexity of both )
I was building a simple TileViewer, so a picture is selected in openFileDialog and set as tiled BackgroundImage of the Form.
My problem is:
I've been using a timer ( interval = 500 ) to reload the image, so if i edit the image in photoshop, the TileViewer will automaticaly reload the updated image ( as i save it in photoshop ).
BUT the problem is that photoshop doesnt have premissions to do so, cuz the image is opened in an other programm ( my TileViewer )
I removed the Timer and made a refresh button instead. but its the same problem.
I thought of making a copy of the bitmap data, but then I get an error that I dont have enought memory for a 32px x 32px img.
My code :
private string FileName;
public Form1() {
InitializeComponent();
FileName = "";
}
// OPEN FILE btn
private void button1_Click( object sender, EventArgs e ) {
openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg";
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
button2.Enabled = true;
FileName = openFileDialog1.FileName;
setImage();
}
}
// REFRESH btn
private void button2_Click( object sender, EventArgs e ) {
if( FileName != "" ) {
setImage();
}
}
private void setImage() {
Bitmap tempImg = new Bitmap( FileName );
Rectangle rect = new Rectangle(0, 0, 100, 100);
PixelFormat format = tempImg.PixelFormat;
this.BackgroundImage = new Bitmap( FileName ).Clone( rect, format );
}
So guys, if you have any suggestions or solutions, let me know.
Update 2:
Another issue was is in line Rectangle rect = new Rectangle(0, 0, 100, 100);
where in the constructor you should pass the width and heigth of the new image not an arbitary value like 100.
thats why the runtime says that you're out of memory !! (It did for me and I have a 18 GB monster machine)
Update:
your're leaking objects hence the out of memory exception
(Haans Already warned you about disposing objects thats.)
Try the following code
private string FileName;
public Form1()
{
InitializeComponent();
FileName = "";
}
// OPEN FILE btn
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog1.FileName;
setImage();
}
}
// REFRESH btn
private void button2_Click(object sender, EventArgs e)
{
if (FileName != "")
{
setImage();
}
}
private void setImage()
{
Stream str=new FileStream(FileName,FileMode.Open, FileAccess.Read,FileShare.Read);
Bitmap tempImg= new Bitmap(Bitmap.FromStream(str));
str.Close();
using( tempImg)
{
Rectangle rect = new Rectangle(0, 0, tempImg.Width, tempImg.Height);
PixelFormat format = tempImg.PixelFormat;
this.BackgroundImage = new Bitmap(tempImg);
}
}
OLD Answer
Like Jason said
you might need to do somethinglike this in your setimage method
Stream str = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
this.BackgroundImage = Bitmap.FromStream(str);
str.Close();
Note that you're better off closing any stream that you open after you use it.
instead of
private void setImage() {
Bitmap tempImg = new Bitmap( FileName ); <---
Rectangle rect = new Rectangle(0, 0, 100, 100);
PixelFormat format = tempImg.PixelFormat;
this.BackgroundImage = new Bitmap( FileName ).Clone( rect, format );
}
The Bitmap.Clone() method doesn't do what you hope it does. It creates a "shallow" copy. You get a new Bitmap object but it still uses the underlying pixel buffer. Including the memory-mapped file that GDI+ uses to keep the pixel data out of the swap file. Which in turn puts a lock on the file.
You'll need to make a "deep" copy, do so with the Bitmap(Image) constructor. Like this:
private void setImage() {
using (var tempImg = new Bitmap(FileName)) {
this.BackgroundImage = new Bitmap(tempImg);
}
}
The using statement ensures the original image is disposed and the lock gets released.
You will need to open the file manually with ReadOnly flags, and then load the payload of the file into the bitmap by hand.
an alternitive would be to copy the file and then open the copy.
See if you cna run the following code while photoshop has the file open
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

How to load image from isolated storage for secondary tile

I am attempting to populate a secondary tile background with an image saved from the PhotoChooserTask, but for some reason I cannot accomplish this. I have referenced a lot of sites but I have not found the proper implementation. All I do is call PhotoChooserTask, and then on the completed event I save the resulting image to isolated storage to be loaded later. This has worked with a HubTile in my application, but for some reason I cannot append the image to a secondary tile. So far what I have is as follows:
MainPage.xaml.cs
string imageFolder = #"\Shared\ShellContent";
string shareJPEG = "shareImage.jpg";
public MainPage()
{
InitializeComponent();
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
}
public void changePictureMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
try
{
photoChooserTask.Show();
}
catch (System.InvalidOperationException)
{
MessageBox.Show("An error occurred");
}
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//persist the data in isolated storage
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if(!myIsolatedStorage.DirectoryExists(imageFolder))
{
myIsolatedStorage.CreateDirectory(imageFolder);
}
if (myIsolatedStorage.FileExists(shareJPEG))
{
myIsolatedStorage.DeleteFile(shareJPEG);
}
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filePath);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, 173, 173, 0, 100);
fileStream.Close();
}
}
}
private void CreateLiveTile(TileItem item)
{
//IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
var title = item.Title.ToString();
string tileParameter = "Param=" + item.Title.ToString();
ShellTile Tile = CheckIfTileExist(tileParameter); // Check if Tile's title has been used
if (Tile == null)
{
//this is not working?
background = new Uri(#"isostore:/Shared/ShellContent/shareJPEG.png", UriKind.Absolute);
//background = new Uri("isostore:/Shared/ShellContent/shareJPEG.png", UriKind.Absolute);
try
{
var LiveTile = new StandardTileData
{
Title = item.TileName,
BackgroundImage = background, //not working
BackTitle = item.TileName,
BackBackgroundImage = new Uri("/background.png", UriKind.Relative),
BackContent = item.Message,
};
ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile);
}
}
Ultimately, the secondary tile is created but there is no image for the BackgroundImage. How would I properly call the isolated strorage path to set the BackgroundImage of the secondary tile accordingly? Or is there something else I should be doing or change?
MainPage.xaml.cs
string imageFolder = #"\Shared\ShellContent";
string shareJPEG = "shareImage.jpg";
...
private void CreateLiveTile(TileItem item)
{
var title = item.Title.ToString();
string tileParameter = "Param=" + item.Title.ToString();
ShellTile Tile = CheckIfTileExist(tileParameter); // Check if Tile's title has been used
if (Tile == null)
{
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
background = new Uri(#"isostore" + filePath, UriKind.Absolute); //this worked
...
}
}
Are you sure the image is saved successfully and exists? You save it as jpeg but you reference a png file. Try #"\Shared\ShellContent\shareJPEG.png"
first you should put your image at "\Shared\ShellContent" location. you can use .png or .jpg file
string imageFolder = #"\Shared\ShellContent";
string shareJPEG = "shareImage.jpg";
...
private void CreateLiveTile(TileItem item)
{
var title = item.Title.ToString();
string tileParameter = "Param=" + item.Title.ToString();
ShellTile Tile = CheckIfTileExist(tileParameter);
if (Tile == null)
{
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(filePath)) // check file exist or not
background = new Uri(#"isostore:" + filePath, UriKind.Absolute);
}
...
}
}

Categories