如何将Windows商店应用程序的tiff文件发送到wpf中的Windows桌面应用程序?

时间:2014-04-07 11:21:58

标签: c# wpf pdf windows-store-apps tiff

我有一个用c#编写的windows store app(8.1),它使用MS pdf API打开一个pdf文件。

在对pdf进行一些注释后,文件被编码为tiff文件。

使用几张wpf画布(每页一张)展示pdf文件。

稍后会渲染这些画布,并将其像素编码为tiff文件。

相关代码:

 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);
    }

这就是SendToServer(使用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();

            }
}

现在问我的问题: 1.如何将此tiff文件发送到.net Windows应用程序? 我应该以哪种格式发送? 2.我收到后如何解码发送回tiff文件的数据? (注释:tiff文件稍后会转换回pdf文件)。

我尝试将FileStream作为字节数组发送,我也尝试将像素作为字节数组发送。数据发送成功,但我没有成功将其解码回tiff文件。

类似的东西:

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

        Image returnImage = Image.FromStream(ms);

        return returnImage;}

Image.FromStream始终失败:

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

Additional information: Parameter is not valid.
请帮助... 谢谢, Adiel。

0 个答案:

没有答案