从本地加载图片
<dxe:ImageEdit Name="iePortrait" Height="120" Width="100"
Stretch="Uniform" ToolTip="{DynamicResource ResourceKey=ResPortrait}"
Cursor="Hand" ShowMenu="False"></dxe:ImageEdit>
public class OpenPictureCommand : Command<RegistrationEditor>
{
protected override void Executed(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
//EventBinder.BindControls(ofd); // added
ofd.Filter = "(*.jpg)|*.jpg";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileName = ofd.FileName;
Console.WriteLine(fileName);
BitmapImage img = new BitmapImage(new Uri(fileName));
Owner.iePortrait.EditValue = img;
}
}
}
Save :
public string BitmapimageToBase64(BitmapImage bi)
{
if (bi == null) return "";
byte[] b = this.getJPGFromImageControl(bi);
string base64String = Convert.ToBase64String(b);
return base64String;
} public byte[] getJPGFromImageControl(BitmapImage imageC)
{
MemoryStream memStream = new MemoryStream();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageC));
encoder.Save(memStream);
return memStream.GetBuffer();
}
Load:
iePortrait.EditValue = this.Base64ToBitmapImage(selectedPortrait.Portrait);
public BitmapImage Base64ToBitmapImage(string str)
{
if (str == null || str=="") return null;
byte[] imageBytes = Convert.FromBase64String(str);
BitmapImage bitmapImage = new BitmapImage();
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
return bitmapImage;
}