OpenFileDialog 打开图片存储到电脑本地上
替换图片
private void btnSkin_Click(object sender, RoutedEventArgs e)
{
string fName;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图像文件(*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp";
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == true)
{
fName = ofd.FileName;
FileStream fs = new FileStream(fName, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string path = AppDomain.CurrentDomain.BaseDirectory + "Background\\主界面.jpg";
FileStream fs2 = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs2);
bw.Write(buffer);
bw.Close();
fs.Close();
LoadImage();
}
}
加载图片时候
private void LoadImage()
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Background\\主界面.jpg";
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(path);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
ImageBrush imageBrush = new ImageBrush();
ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
imageBrush.ImageSource = (ImageSource)imageSourceConverter.ConvertFrom(stream);
grids.Background = imageBrush;
bitmap.Dispose();
}