Wednesday, December 28, 2011

WPF Image Uploading & displaying

private void btnbrowse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog objFileDailog = new OpenFileDialog();
if (objFileDailog.ShowDialog() == true)
{
txtFileName.Text = objFileDailog.FileName.ToString();
BitmapImage bit = new BitmapImage();


var path = Directory.GetParent("~/Upload/").ToString();
path = path.Replace(@"bin\Debug\~\", "");
path = path + "\\" + objFileDailog.SafeFileName;


using (FileStream fs = File.Create(path))
{
SaveFile(objFileDailog.OpenFile(), fs);
}

MessageBox.Show("success");

bit.BeginInit();

//bit.StreamSource = objFileDailog.OpenFile();
bit.UriSource = new Uri(path, UriKind.Absolute);
bit.EndInit();

img.Source = bit;
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}

public bool IsReusable
{
get
{
return false;
}
}

No comments:

Post a Comment