Databinding on the image, but I need a byteArray

I work with Winforms, CAB, C # and Infragistics. I am trying to work with MVP with my WCF backend.

In my presenter, I have My Model, let me call it the DataContract Agreement. This data contract contains many attributes:

... [DataMember] public byte[] PVImage { get; set; } [DataMember] public byte[] OntwerpImage { get; set; } [DataMember] public Decimal WattpiekPrijs { get; set; } ... 

You will notice that the image is stored as byte []. I bind these attributes to the controls of my user control:

  BindingHelper.BindField(_ultraPictureBoxPV, "Image", _bindingSource, "PVImage"); BindingHelper.BindField(_ultraPictureBoxOntwerp, "Image", _bindingSource, "OntwerpImage"); 

BindingHelper simply adds a BindingContext to the specified controller (control.BindingContext.Add (...)).

In any case, the problem is: the data contract contains the image as a bytearray, while I snap to the image. This makes the attribute remain โ€œnullโ€ because it does not want to put the image in a byteArray;)

I tried playing with him, but I think I have two options:

  • Can I try to use a kind of converter? Therefore, when an image is inserted, it is transmitted as a byteArray instead of the image in Model (= data binding).

  • I can remove the binding and make an event when the form is โ€œsubmittedโ€, and convert the image to byteArray and populate the model. (= no data binding)

TL DR; Do you know a way to "convert" an image to bytes when it is transferred to data binding?

Hope my question is clear! thanks for the help

+6
source share
1 answer

I would add a new property of type Image that will bind to your UltraPictureBox control. Add two methods that can convert in any direction.

 [DataMember] public Image OntwerpImageImage { get { return ConvertByteArrayToImage(OntwerpImage); } set { OntwerpImage = ConvertImageToByteArray(value); } } //[DataMember] public byte[] OntwerpImage { get; set; } public Image ConvertByteArrayToImage(Byte[] bytes) { var memoryStream = new MemoryStream(bytes); var returnImage = Image.FromStream(memoryStream); return returnImage; } public Byte[] ConvertImageToByteArray(Image image) { var memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); return memoryStream.ToArray(); } 
+3
source

Source: https://habr.com/ru/post/897236/


All Articles