0

I have a windows store app (8.1) written in c# that opens a pdf file using the MS pdf API.

After some annotations are made to the pdf the file is encoded to a tiff file.

The pdf file is presented using several wpf Canvases (one per page).

These canvases are later rendered and their pixels are encoded to the tiff file.

The relevant code :

 private async void CreateSaveBitmapAsync(List<Canvas> results)
    {
        Client client = new Client("127.0.0.1", "13000");

        var picker = new FileSavePicker();
        picker.FileTypeChoices.Add("Tiff Image", new string[] { ".tiff" });
        StorageFile file = await picker.PickSaveFileAsync();

        if (file == null)
        {
            return;
        }

        using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.TiffEncoderId, stream);

            for (int i = 0; i < results.Count; i++)
            {
                Canvas canvas = results[i];

                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(canvas);

                if (canvas != null)
                {
                    if (file != null)
                    {
                        var pixels = await renderTargetBitmap.GetPixelsAsync();

                        byte[] bytes = pixels.ToArray();

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             (uint)canvas.Width, (uint)canvas.Height,
                                             96, 96, bytes);

                        await encoder.GoToNextFrameAsync();
                    }
                }
            }
        }

        client.SendToServer(file);
    }

and this it the SendToServer (using a SocketStream) :

 public async void SendToServer(byte[] bytes)
        {
            if (!_connected)
            {
                Debug.WriteLine("Must be connected to send!");
                return;
            }

            Int32 len = 0; // Gets the UTF-8 string length.

            //while (true)
            //{
            try
            {
                Debug.WriteLine("Trying to send data ...");

                DataWriter writer = new DataWriter(_clientSocket.OutputStream);

                writer.WriteUInt32((uint)(bytes.Length));
                writer.WriteBytes(bytes);

                // Call StoreAsync method to store the data to a backing stream
                await writer.StoreAsync();

                Debug.WriteLine("Data was sent" + Environment.NewLine);

                // detach the stream and close it

                writer.DetachStream();
                writer.Dispose();

            }
}

Now to my question(s) : 1. How can i send this tiff file to a .net windows application ? in which format should i send it ? 2. How can i decode the data sent back to a tiff file after i receive it ? (remark : the tiff file is later converted back to a pdf file).

I tried sending the FileStream as a byte array and i also tried to send the pixels as byte array. The data is sent successfully but i don't succeed to decode it back a tiff file.

Something like that :

public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        ms.Write(byteArrayIn, 0, byteArrayIn.Length);

        Image returnImage = Image.FromStream(ms);

        return returnImage;}

the Image.FromStream always fails on :

An exception of type 'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code

Additional information: Parameter is not valid.

help please ... thanks, Adiel.

Adiel Yaacov
  • 1,401
  • 3
  • 14
  • 24

0 Answers0