1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
private void LogoImg_Click(object sender, EventArgs e)
{
openFileDialog2.Filter = "Fichier image (*.png)|*.png|Fichier image (*.jpg)|*.jpg|All files (*.*)|*.*";
openFileDialog2.FileName = "";
DialogResult result = openFileDialog2.ShowDialog();
this.LogoImg.Image = null;
this.LogoImg.Refresh();
if (result == DialogResult.OK)
{
Console.WriteLine(openFileDialog2.FileName.Substring(openFileDialog2.FileName.LastIndexOf("\\") + 1));
string file = openFileDialog2.FileName.Substring(openFileDialog2.FileName.LastIndexOf("\\") + 1);
int first = openFileDialog2.FileName.IndexOf(file);
string rep = openFileDialog2.FileName.Substring(0, first);
string TargetRep = Application.LocalUserAppDataPath + "\\fichiers";
string sourceFile = rep + "\\" + file;
string destFile = TargetRep + "\\" + "image_logo.png";
CopyBinaryFile(sourceFile, destFile);
using (FileStream FSnewlogo = new FileStream(destFile,FileMode.Open, FileAccess.Read))
{
Image NewLogo = Image.FromFile(FSnewlogo.Name);
LogoImg.Image = NewLogo;
Ecrit_Config("ImgLogo", destFile);
FSnewlogo.Close();
FSnewlogo.Dispose();
}
}
}
static bool CopyBinaryFile(string srcfilename, string destfilename)
{
if (System.IO.File.Exists(srcfilename) == false)
{
Console.WriteLine("Could not find the Source file");
return false;
}
System.IO.Stream s1 = System.IO.File.Open(srcfilename, System.IO.FileMode.Open);
System.IO.Stream s2 = System.IO.File.Open(destfilename, System.IO.FileMode.Create);
System.IO.BinaryReader f1 = new System.IO.BinaryReader(s1);
System.IO.BinaryWriter f2 = new System.IO.BinaryWriter(s2);
while (true)
{
byte[] buf = new byte[10240];
int sz = f1.Read(buf, 0, 10240);
if (sz <= 0)
break;
f2.Write(buf, 0, sz);
if (sz < 10240)
break; // eof reached
}
f1.Close();
f1.Dispose();
f2.Close();
f2.Dispose();
s1.Close();
s1.Dispose();
s2.Close();
s2.Dispose();
return true;
}
private bool Ecrit_Config(string elementkey, string elementvalue)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.GetSection("appSettings");
config.AppSettings.Settings.Remove(elementkey);
config.AppSettings.Settings.Add(elementkey, elementvalue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception)
{
return false;
}
return true;
} |
Partager