English 中文(简体)
。 Net Maui ValueSource Not Work If Converted To Byte Array From Building 观点
原标题:.Net Maui ImageSource Not Working If Converted To Byte Array From DrawingView
  • 时间:2023-09-01 10:55:32
  •  标签:
  • c#
  • .net
  • maui

I m using the CommunityToolkit.Maui.Views.DrawingView to capture a customer signature. Once they have finished i hide the DrawingView and display it as in image, so as to not interfere with scrolling. The code used to switch between the editor and the image is:

private async void ElEditSignatureButton_Clicked(object sender, EventArgs e)
{
    if (!EditingSignature)
    {
        ElCustomerSignatureImage.IsVisible = false;
        ElCustomerSignatureDraw.IsVisible = true;
        ElEditSignatureButton.Source = ImageSource.FromFile("tick.png");
        EditingSignature = true;
    }
    else
    {
        if (ElCustomerSignatureDraw.Lines.Count > 0)
        {
            Stream stream = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

            byte[] bytes = new byte[stream.Length];
            stream.Write(bytes, 0, bytes.Length);
            SignatureImage = bytes;

            ElCustomerSignatureImage.Source = ImageSource.FromStream(() => new MemoryStream(bytes));

            ElCustomerSignatureDraw.IsVisible = false;
            ElCustomerSignatureImage.IsVisible = true;
            ElEditSignatureButton.Source = ImageSource.FromFile("pencil.png");
            EditingSignature = false;
        }
        else
        {
            await DisplayAlert("Customer Report", "Signature Required", "Ok");
        }
    }
}

然而,在这样做时,没有显示这种形象。

If i change it to remove the convertion to byte[] (for use later when submitting) the image works:

Stream stream = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

ElCustomerSignatureImage.Source = ImageSource.FromStream(() => stream);

在撰写本报告时,我发现:

Stream stream = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

ElCustomerSignatureImage.Source = ImageSource.FromStream(() => stream);

Stream stream2 = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

byte[] bytes = new byte[stream2.Length];
stream2.Write(bytes, 0, bytes.Length);
SignatureImage = bytes;

Does anyone know why the first example doesn t work? I mean a stream of bytes should just be a stream of bytes regardless of it s method of creation.

EDIT: 矫正,即说行之有效的最后选择, by变具有正确的长度,但数据是所有0, 认为也是第一个例子的问题。

问题回答

Ok After much trial and error i found code that poplated both the byte array and the image control correctly:

Stream stream = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

MemoryStream temp = new MemoryStream();
stream.CopyTo(temp);
SignatureImage = temp.ToArray();

stream.Position = 0;
ElCustomerSignatureImage.Source = ImageSource.FromStream(() => stream);

因此,出于某种原因,《记忆》。 ToArray()工程,但记忆犹新。 没有任何理由。

你们可以尝试利用这一渠道。 阅读而不是流。 撰写:

Stream stream = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

ElCustomerSignatureImage.Source = ImageSource.FromStream(() => stream);

Stream stream2 = await ElCustomerSignatureDraw.GetImageStream(ElCustomerSignatureImage.Width, ElCustomerSignatureImage.Height);

byte[] bytes = new byte[stream2.Length];
stream2.Read(bytes, 0, bytes.Length);
SignatureImage = bytes;

注: Length > int.MaxValue, 这将造成错误或数据损失。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签