c# - How do I close an image open in a picturebox so I can delete it? -


at moment form has system can save data in form directory in applications folder load again through listbox have set need add delete button delete data out of folder problem if opened it returns error cannot delete because file in use heres code use save data

    private void button3_click(object sender, eventargs e)     {             using (var writer = new streamwriter(@"cards\" + cardname.text + ".card", false))             {                 int lev = level.selectedindex;                 int race = race.selectedindex;                 int attribute = cardattribute.selectedindex;                 int cardtype = combobox1.selectedindex;                 int monstertype = combobox2.selectedindex;                 int spelltype = combobox4.selectedindex;                 int traptype = combobox3.selectedindex;                 string[] des = carddescription.text.split('\n');                 imageconverter img_converter = new imageconverter();                 byte[] bytes = (byte[])img_converter.convertto(img, typeof(byte[]));                   file.writeallbytes(@"cards\" + cardname.text + ".jpg", bytes);                 writer.writeline("#id:" + cardid.text);                 writer.writeline("#lev:" + lev);                 writer.writeline("#rac:" + race);                 writer.writeline("#att:" + attribute);                 writer.writeline("#atk:" + atk.text);                 writer.writeline("#def:" + def.text);                 writer.writeline("#pic:" + cardname.text + ".jpg");                 writer.writeline("#ctp:" + cardtype);                 writer.writeline("#mtp:" + monstertype);                 writer.writeline("#stp:" + spelltype);                 writer.writeline("#ttp:" + traptype);                 writer.writeline("#gol:" + checkbox1.checked);                 writer.writeline("#nam:" + cardname.text);                 foreach (string l in des)                 {                     writer.writeline("#des:" + l);                 }             }             listupdate();         } 

to load data

    private void listbox1_doubleclick(object sender, eventargs e)     {         string open = @"cards\" + listbox1.selecteditem.tostring() + ".card";         var reader = new streamreader(file.openread(open));         while (!reader.endofstream)         {             string line = reader.readline();             if (line.startswith("#id:"))             {                 cardid.text = line.substring(4);             }             if (line.startswith("#lev:"))             {                 string lev = line.substring(5);                 level.selectedindex = convert.toint32(lev);             }             if (line.startswith("#rac:"))             {                 string rac = line.substring(5);                 race.selectedindex = convert.toint32(rac);             }             if (line.startswith("#att:"))             {                 string att = line.substring(5);                 cardattribute.selectedindex = convert.toint32(att);             }             if (line.startswith("#atk:"))             {                 atk.text = line.substring(5);             }             if (line.startswith("#def:"))             {                 def.text = line.substring(5);             }             if (line.startswith("#ctp:"))             {                 string ctp = line.substring(5);                 combobox1.selectedindex = convert.toint32(ctp);             }             if (line.startswith("#mtp:"))             {                 string mtp = line.substring(5);                 combobox2.selectedindex = convert.toint32(mtp);             }             if (line.startswith("#stp:"))             {                 string stp = line.substring(5);                 combobox4.selectedindex = convert.toint32(stp);             }             if (line.startswith("#ttp:"))             {                 string ttp = line.substring(5);                 combobox3.selectedindex = convert.toint32(ttp);             }             if (line.startswith("#gol:"))             {                 string gold = line.substring(5);                 checkbox1.checked = convert.toboolean(gold);             }             if (line.startswith("#nam:"))             {                 cardname.text = line.substring(5);             }             if (line.startswith("#des:"))             {                 des += line.substring(5) + environment.newline;                 carddescription.text = des;             }             if (line.startswith("#pic:"))             {                 if (file.exists(@"cards\" + line.substring(5)))                 {                     img = image.fromfile(@"cards/" + line.substring(5));                 }             }         }           des = "";         generatecard();     } 

and delete it

    private void button4_click(object sender, eventargs e)     {         string list = listbox1.selecteditem.tostring();         if (file.exists(@"cards\" + list + ".card"))         {             file.delete(@"cards\" + list + ".card");         }         if (file.exists(@"cards\" + list + ".jpg"))         {             file.delete(@"cards\" + list + ".jpg");         }         listupdate();     } 

okay, problem picturebox using image want delete... right? so, instead of using picture local drive, load picture picturebox:

public bitmap loadbitmap(string path) {     if (file.exists(path))     {         // open file in read mode         using (filestream stream = new filestream(path, filemode.open, fileaccess.read))         // binary reader file stream         using (binaryreader reader = new binaryreader(stream))         {             // copy content of file memory stream             var memorystream = new memorystream(reader.readbytes((int)stream.length));             // make new bitmap object owner of memorystream             return new bitmap(memorystream);         }     }     else     {         messagebox.show("error loading file.", "error!", messageboxbuttons.ok);         return null;     } } 

to use it, call:

picturebox1.image = loadbitmap("location"); 

now can delete picture without locking. beware, `picturebox not cleared. you'll have clear manually.

i hope helps. ;)


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -