简体   繁体   中英

How to save image in Silverlight

Hi I have created a Silverlight application that allows the user to type their name, select date and sign their name, (a signature strip). I am looking to add to a webform I have already created. I build the signature strip using the borderInk and inkP tool in silverlight on a grid. However i don't know how to save the image. I want to store it in a database, I have already created. I also want to attach the silverlight application the the webforms I have created. Any help on how to do this??

You should use WCF service and save the image in server using Bytes Stream.

Here is an example,

Upload image in silverlight WCF

You should render your drawing surface element (Grid) to the Bitmap and save the result.

The sample method which should get an element and return bytes of the jpeg image

private byte[] RenderToJpeg(FrameworkElement element)
{
    using (var stream = new MemoryStream())
    {
        var bmp = new WriteableBitmap(element, null);
        bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 90);

        stream.Flush();
        return stream.ToArray();
    }
}

If you are saving image using OpenFileDialog ,this could helps you.

 decimal _imagementSize = 0;
 string _imageName = "";
 string _imageType = "";
 Binary _image;
 OpenFileDialog dialog=new OpenFileDialog();

 private void btnSaveImage_Click(object sender, RoutedEventArgs e)
 {
   dialog.Multiselect = false;
   dialog.Filter = "All Files | *.*";
   if (dialog.ShowDialog() == true)
   {
       bool fileExist = dialog.File.Exists;
       if (fileExist)
       {       
          UploadFile();    
       }                  
   }
 }

 private void UploadFile()
 {
    double fileLength = 0;
    var stream = dialog.File.OpenRead();
    var bnr = new BinaryReader(stream);
    byte[] buffer = new byte[stream.Length + 1];
    buffer = bnr.ReadBytes((int)stream.Length);
    fileLength = stream.Length;
    _imageName = dialog.File.Name;
    _imageType = dialog.File.Extension;
    _imageSize = (decimal)(fileLength / 1024);
    _image = new Binary() { Bytes = buffer };         
 }

and if you are using WCF service to save image,you just send _image

and WCF method like

[OperationContract]
public void SaveImage(System.Data.Linq.Binary _image)
{
   //save image to DB or enything
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM