1

Today I have been trying to get an image to save into a database, and I really can't figure it out.

I have made the following table (Afbeeldingen):

  • id:int
  • afbeelding1:varbinary(max)

Imported it with a Linq-to-SQL class, wrote a WCF service for it:

    [OperationContract]
    public void setAfbeelding(Afbeelding a)
    {
        dc.Afbeeldings.InsertOnSubmit(a);
        dc.SubmitChanges();
    }

And then in my xaml page I try to to create an Afbeelding, but I can't put the Byte[] as a varbinary. I don't know how to do this and I can't seem to find anything about it.

OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.Filter = "JPEG files|*.jpg";

if (openFileDialog.ShowDialog() == true)
{
   Stream stream = (Stream)openFileDialog.File.OpenRead();
   Byte[] bytes = new Byte[stream.Length];
   stream.Read(bytes, 0, (int)stream.Length);
   string fileName = openFileDialog.File.Name;

   Afbeelding a = new Afbeelding();
   a.id = 1;
   a.afbeelding1 = new Byte{ Bytes = bytes};
}

I hope somebody can help me because I really can't figure this out.

Thank you in advance, Thomas

Edit: Having solved this problem, what happens now when I press the button as I get an error.

System.ServiceModel.FaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :a. The InnerException message was 'There was an error deserializing the object of type OndernemersAward.Web.Afbeelding. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'.  Please see InnerException for more details.
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndsetAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndsetAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndsetAfbeelding(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
Schoof
  • 2,715
  • 5
  • 29
  • 41

1 Answers1

2

You need to construct a Binary object.

a.afbeelding1 = new Binary( bytes );
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Thank you, but when I try this I get the error: `'OndernemersAward.EditAfbeeldingServiceReference.Binary' does not contain a constructor that takes 1 arguments` – Schoof Nov 25 '11 at 19:59
  • It should be a `System.Linq.Binary` -- perhaps you've created a class for it? – tvanfosson Nov 25 '11 at 20:02
  • I just did a search for afbeelding1 and I couldn't find it anywhere (but the linq class), this is the linq class: http://pastebin.com/n91W2fme Edit: There is another one, in reference.cs: http://pastebin.com/jDA07rUs (I don't know why), I also don't know why it's called afbeelding1, because in my database it is just afbeelding. – Schoof Nov 25 '11 at 20:05
  • I just renamed the column to "source", addded it again to my Dataclasses and it still tells me that it doesn't contain a constructor that takes 1 arguments. This doesn't make any sense :S – Schoof Nov 25 '11 at 20:13
  • That's odd. I wonder why it created the Binary class for the service. If it persists in doing that, use `a.afbeelding1 = new Binary { Bytes = bytes };` – tvanfosson Nov 25 '11 at 20:36
  • I did have to adjust the reference because of some weird error, asked here: http://stackoverflow.com/questions/8273959/service-reference-custom-tool-warning-cannot-import-wsdlporttype But that seemed to done the trick, thank you :) – Schoof Nov 25 '11 at 20:39
  • Thank you, this worked, but now I am getting an error when trying to save the image. :( – Schoof Nov 25 '11 at 20:47
  • I don't see any reason why it should be creating a proxy for the `Binary` class as it is marked with the Serializable and DataContract attributes. – tvanfosson Nov 25 '11 at 21:06
  • This is still causing me problems now, when trying to convert the binary back to an image, I have to reference to the Binary class but it references to my ServiceReference... – Schoof Nov 26 '11 at 15:31