{"id":179,"date":"2016-09-13T14:23:45","date_gmt":"2016-09-13T14:23:45","guid":{"rendered":"http:\/\/vladymix.es\/?p=179"},"modified":"2016-09-13T14:23:45","modified_gmt":"2016-09-13T14:23:45","slug":"irandomaccessstream-to-writeablebitmap","status":"publish","type":"post","link":"https:\/\/vladymix.es\/wordpress\/index.php\/2016\/09\/13\/irandomaccessstream-to-writeablebitmap\/","title":{"rendered":"IRandomAccessStream to WriteableBitmap"},"content":{"rendered":"<p>Bueno, despu\u00e9s de una larga b\u00fasqueda en Internet no consegu\u00eda solucionar el error en UWP.<\/p>\n<p>Pense que era algo tribial pero no.<\/p>\n<p>Siempre que hacia un \u00abPixelBuffer\u00bb\u00a0para leerlo como stream, fallaba el c\u00f3digo y saltaba una excepci\u00f3n de tipo\u00a0\u00abNo se encuentra el componente. (Excepci\u00f3n de HRESULT: 0x88982F50)\u00bb.<\/p>\n<pre class=\"lang:c# decode:true\" title=\"definition\">var stream = writeableBitmap.PixelBuffer.AsStream();<\/pre>\n<p>Como ya estamos acostumbrados a que Microsoft. nos detalle el error fue \u00abFacil\u00bb buscar la soluci\u00f3n. XD.<\/p>\n<p>Bueno al final la soluci\u00f3n fue inicializar el objeto dos veces.<\/p>\n<p>He aqu\u00ed las lineas de c\u00f3digo.<\/p>\n<pre class=\"lang:c# decode:true\" title=\"IRandomAccessStream to WriteableBitmap\"> public WriteableBitmap StreamToWriteableBitmap(IRandomAccessStream stream)\r\n        {   \r\n            \/\/ initialize with 1,1 to get the current size of the image\r\n            var writeableBmp = new WriteableBitmap(1, 1);\r\n            writeableBmp.SetSource(stream);\r\n\r\n            \/\/ and create it again because otherwise the WB isn't fully initialized and decoding\r\n            \/\/ results in a IndexOutOfRange\r\n            writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);\r\n\r\n            stream.Seek(0);\r\n            writeableBmp.SetSource(stream);\r\n\r\n           return writeableBmp;\r\n        }<\/pre>\n<p>Y cuidadito si se les olvida el .Seek(0).<\/p>\n<p>De esta manera he conseguido solucionar el error de convertir un IRandomAccesStream to WriteableBitmap para poder guarda<\/p>\n<h3>Bueno, se preguntaran \u00bfY que hago con un WriteableBitmap?.<\/h3>\n<p>Pues surgi\u00f3 de la necesidad de guardar una hoja de un pdf a imagen con una resoluci\u00f3n mayor.<\/p>\n<p>Aqu\u00ed el c\u00f3digo para ello.<\/p>\n<pre class=\"lang:c# decode:true \" title=\"SaveFileJPG\">public async Task SaveToFile(WriteableBitmap writeableBitmap, IStorageFile outputFile)\r\n        {\r\n            try\r\n            {\r\n                var stream = writeableBitmap.PixelBuffer.AsStream();\r\n\r\n                var pixels = new byte[(uint)stream.Length];\r\n                await stream.ReadAsync(pixels, 0, pixels.Length);\r\n\r\n                using(var streamOut = await outputFile.OpenAsync(FileAccessMode.ReadWrite))\r\n                {\r\n                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, streamOut);\r\n                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96, 96, pixels);\r\n\r\n                    await encoder.FlushAsync();\r\n\r\n                    using(var outputStream = streamOut.GetOutputStreamAt(0))\r\n                    {\r\n                        await outputStream.FlushAsync();\r\n                    }\r\n                }\r\n            }\r\n            catch(Exception ex)\r\n            {\r\n                Debug.WriteLine(ex.Message);\r\n            }\r\n        }<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bueno, despu\u00e9s de una larga b\u00fasqueda en Internet no consegu\u00eda solucionar el error en UWP. Pense que era algo tribial&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-c","category-windows-store-and-phone"],"_links":{"self":[{"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":5,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vladymix.es\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}