tag:blogger.com,1999:blog-563071785757928434.post2948888175913038694..comments2024-02-21T08:31:04.853+01:00Comments on Kodierer [Coder]: Convert, Encode And Decode Silverlight WriteableBitmap DataRene Schultehttp://www.blogger.com/profile/12777157871967896549noreply@blogger.comBlogger52125tag:blogger.com,1999:blog-563071785757928434.post-18955596919194854132013-01-22T08:56:54.037+01:002013-01-22T08:56:54.037+01:00C# <-> VB.Net
http://converter.telerik.comC# <-> VB.Net <br />http://converter.telerik.comRene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-54206290994478225722013-01-22T07:46:13.353+01:002013-01-22T07:46:13.353+01:00please convert this part into vb.net
public stati...please convert this part into vb.net<br /> public static void EncodeJpeg(this WriteableBitmap bitmap, MemoryStream stream, int quality = 90)<br /> {<br /> int width = bitmap.PixelWidth;<br /> int height = bitmap.PixelHeight;<br /> int bands = 3;<br /> byte[][,] raster = new byte[bands][,];<br /> for (int i = 0; i < bands; i++)<br /> {<br /> raster[i] = new byte[width, height];<br /> }<br /> for (int row = 0; row < height; row++)<br /> {<br /> for (int column = 0; column < width; column++)<br /> {<br /> int pixel = bitmap.Pixels[width * row + column];<br /> raster[0][column, row] = (byte)(pixel >> 16); //R<br /> raster[1][column, row] = (byte)(pixel >> 8); //G<br /> raster[2][column, row] = (byte)pixel; //B<br /> }<br /> }<br /> ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };<br /> FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);<br /><br /> JpegEncoder encoder = new JpegEncoder(img, quality, stream);<br /> encoder.Encode();<br /><br /> stream.Seek(0, SeekOrigin.Begin);<br /><br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-81972057865933907502012-11-19T20:52:40.381+01:002012-11-19T20:52:40.381+01:00Hey Stanley,
I'm not aware there's someth...Hey Stanley,<br /><br />I'm not aware there's something like this built-in. You can probably make your own heuristics, but in the end the best result is just computing the size in the background. See how pro photo editing software handles it.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-62944166497264443792012-11-19T20:49:09.259+01:002012-11-19T20:49:09.259+01:00Hello Rene!
Using EncodeJpeg method is it possibl...Hello Rene!<br /><br />Using EncodeJpeg method is it possible to calculate beforehand the second argument "quality" in JpegEncoder constructor? I mean if I have a max weight for final jpeg image and if while encoding it with Encode method of JpegEncoder I get the weight bigger than required I need to resize the image again and again lowing down the quality parameter. So are there ways to figure out the quality parameter?<br /><br />StanleyAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-47898583725978944752012-08-10T09:34:53.760+02:002012-08-10T09:34:53.760+02:00Sure you can, you just have to implement the BMP s...Sure you can, you just have to implement the BMP spec which should be rather easy. I guess there's an implementation available out there you can re-use. The WriteableBitmapEx also has a WriteTga extension method you can use. Tga is similar o bmp but supports the alpha channel.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-42626193713685391872012-08-10T09:21:50.271+02:002012-08-10T09:21:50.271+02:00Hello!
Can I save an image to BMP format?Hello!<br />Can I save an image to BMP format?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-11237058805562310752012-08-03T11:39:46.710+02:002012-08-03T11:39:46.710+02:00Hello Rene,
I tried to implement the conversion o...Hello Rene,<br /><br />I tried to implement the conversion of the WriteableBitmap to an ARGB byte array that you posted but I can't seem to find the Pixels property. (int[] p = bmp.Pixels;)<br />Can you tell me what's the problem?<br /><br />Thank you.Irinanoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-80139925070844296052012-02-16T10:31:47.011+01:002012-02-16T10:31:47.011+01:00Hey Illya,
You can find all you need in the above...Hey Illya,<br /><br />You can find all you need in the above blog post in the DecodeJpeg method. Just remove the part where the WriteableBitmap is created and the pixels are copied into it.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-79518528871676385732011-10-05T21:40:34.777+02:002011-10-05T21:40:34.777+02:00Just read THiS blog post.Just read THiS blog post.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-77575194696788265112011-10-05T21:32:02.654+02:002011-10-05T21:32:02.654+02:00Hi Rene
You say to Nero that he must encode it on...Hi Rene<br /><br />You say to Nero that he must encode it on the client side to a JPEG, not just a byte array of the raw ARGB .How can I do that I have the same problem.alexghttps://www.blogger.com/profile/08749292305738252463noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-81152816378124311062011-09-22T00:36:11.788+02:002011-09-22T00:36:11.788+02:00thanks a LOT man, it's working. my first silve...thanks a LOT man, it's working. my first silverlight app & my first wcf service, and it's WORKIN' :DNerohttps://www.blogger.com/profile/10755410729127587693noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-18754580996573648722011-09-21T19:32:41.776+02:002011-09-21T19:32:41.776+02:00You have to encode it on the client side to a JPEG...You have to encode it on the client side to a JPEG, not just a byte array of the raw ARGB data. The Image.FromStream and BitmapImage expect a JPEG.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-16798733033056247352011-09-21T17:18:54.337+02:002011-09-21T17:18:54.337+02:00hello Rene,
I was working on sending a webcam-capt...hello Rene,<br />I was working on sending a webcam-captured image to the server using WCF service & i was using this ToByteArray() method<br /><br />//CLIENT Side<br /><br />byte[] byteArray = writeableBitmapSource.ToByteArray();<br /><br />ImageGenerateServiceClient serviceClient = new ImageGenerateServiceClient();<br />serviceClient.GenerateCompleted += new EventHandler(serviceClient_GenerateCompleted);<br /> serviceClient.GenerateAsync(byteArray);<br /><br />//SERVER Side<br /><br />MemoryStream memoryStream = new MemoryStream(byteArray);<br />Image image = Image.FromStream(memoryStream);<br /><br />...then I was using image.Save() method. But what i get is "Parameter is not valid." execption :(<br /><br />...then for testing purpose i used the returned byte[] to provide the Source of an Image control as follows<br /><br />MemoryStream memoryStream = new MemoryStream(byteArray); <br />BitmapImage bitmapImage = new BitmapImage();<br />bitmapImage.SetSource(memoryStream);<br />imgPreview.Source = bitmapImage;<br /><br />...but this time i got "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))" exception :( :(<br /><br />what did i miss? can you help?Nerohttps://www.blogger.com/profile/10755410729127587693noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-53441681906611582162011-05-11T03:36:18.493+02:002011-05-11T03:36:18.493+02:00Hi Rene,
Great article.
Could I still use this m...Hi Rene,<br /><br />Great article.<br /><br />Could I still use this method to encode the video captured with the webcam?<br /><br />Thanks in advance.Anonymoushttps://www.blogger.com/profile/03382442593717909014noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-84437746997215314402011-02-28T02:15:56.824+01:002011-02-28T02:15:56.824+01:00Hi Rene
Love your articles, thanks they help me a...Hi Rene<br /><br />Love your articles, thanks they help me a lot.<br /><br />One point that I would like to request...<br /><br />I know all developers have fantastic memory and know by instinct what name-space to use for all code, but... for us lesser developer types could you add the name-space<br /><br />Many thanks<br /><br />MrBronzmrbronzhttps://www.blogger.com/profile/10831140221489425357noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-85670090244465609232011-02-13T19:30:32.328+01:002011-02-13T19:30:32.328+01:00Nope, the Pixels property has a setter.Nope, the Pixels property has a setter.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-55119787816219049302011-02-13T07:15:52.567+01:002011-02-13T07:15:52.567+01:00Buffer.BlockCopy(buffer, 0, bmp.Pixels, 0, buffer....Buffer.BlockCopy(buffer, 0, bmp.Pixels, 0, buffer.Length);<br />This will not work ( Phone 7 ) because bmp.Pixels is a readonly property and it can not be assigned.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-4020860280277482011-01-22T02:41:27.916+01:002011-01-22T02:41:27.916+01:00The ToByteArray method seems to be exactly what I ...The ToByteArray method seems to be exactly what I need for my app. Unfortunately when I save the byte[] to a file, it's not creating a valid image. (Similarly if I upload the byte[] to a SharePoint document library, the image is invalid as well.)<br /><br /> I've validated that the WriteableBitmap is valid. The new file does consist of 153,600 bytes, which equals the height x width x 4, which I'm guessing is right. Any suggestions on what else I may be able to do to debug? I'm assuming the methods simply works "as is" and doesn't require and changes.Attila Buturlahttps://www.blogger.com/profile/11275441997050779899noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-81321609428436230822010-10-18T15:03:51.973+02:002010-10-18T15:03:51.973+02:00Rene,
I didn't think about the artifacts. TH...Rene,<br /><br />I didn't think about the artifacts. THanks!Davidhttp://www.coderforrent.comnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-56111499493757722782010-10-18T08:49:31.061+02:002010-10-18T08:49:31.061+02:00Good catch David. Yes, the Decode is reversed. I m...Good catch David. Yes, the Decode is reversed. I must admit that I always use the built-in BitmapImage class and its SetSource method for JPEG decoding.<br /><br />Another thing about the IValueConverter. Don't do this subsequently with the same image. JPEG is a lossy compression and will result in image artifacts if the encode is performed too often. You should rather save the original stream if it is already encoded.Rene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-71904463642507488152010-10-18T06:01:18.766+02:002010-10-18T06:01:18.766+02:00AHA!
You accidentally reversed the interval of x ...AHA!<br /><br />You accidentally reversed the interval of x and y... it should be :<br /><br /> for (int y = 0; y < h; y++)<br /> {<br /> for (int x = 0; x < w; x++)<br /> {<br /> <br /> p[i++] = (0xFF << 24) // A <br /> | (pixelsFromJpeg[0][x, y] << 16) // R <br /> | (pixelsFromJpeg[1][x, y] << 8) // G <br /> | pixelsFromJpeg[2][x, y]; // B <br /> <br /> }<br /> }Davidhttp://www.coderforrent.comnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-10861186682673666202010-10-18T02:13:19.946+02:002010-10-18T02:13:19.946+02:00Jim,
I have the same exact issue.
public class...Jim,<br /><br />I have the same exact issue. <br /><br /> public class ImageByteConverter : IValueConverter<br /> {<br /> /// <br /> /// Converts a Jpeg byte array into a WriteableBitmap<br /> /// <br /> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br /> {<br /> if (value == null)<br /> return null;<br /><br /><br /> byte[] val = (byte[])value;<br /> <br /> MemoryStream ms = new MemoryStream(val);<br /><br /> return WriteableBitmapExtentions.DecodeJpeg(ms);<br /> }<br /><br /><br /><br /> /// <br /> /// Converts a WriteableBitmap into a Jpeg byte array.<br /> /// <br /> public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br /> {<br /> if (value == null)<br /> return null;<br /><br /> WriteableBitmap wb = (WriteableBitmap)value;<br /> return wb.EncodeJpeg();<br /> }<br /> }Davidhttp://www.coderforrent.comnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-40406477303120897602010-10-17T22:07:49.539+02:002010-10-17T22:07:49.539+02:00Thanks heaps. I knew there must have been an libr...Thanks heaps. I knew there must have been an library for this, but I couldn't find it. <br /><br />Regards Ray (not Anonymous)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-60839232712711313232010-10-17T19:51:50.622+02:002010-10-17T19:51:50.622+02:00Hi Anonymous,
Reflection.Emit is not supported by...Hi Anonymous,<br /><br />Reflection.Emit is not supported by the .Net Compact Framework, which is the underlying runtime on Windows Phone. <br />You should use LoadJpeg and SaveJpeg to decode / encode a WriteableBitmap load / save as JPEG stream.<br />http://msdn.microsoft.com/en-us/library/ff707350(v=VS.92).aspxRene Schultehttps://www.blogger.com/profile/12777157871967896549noreply@blogger.comtag:blogger.com,1999:blog-563071785757928434.post-24764474256353083142010-10-17T12:55:11.267+02:002010-10-17T12:55:11.267+02:00Hi,
Using the above code in Windows Phone 7 works...Hi,<br /><br />Using the above code in Windows Phone 7 works for the encode, but I get the following exception in the decode: System.Reflection.Emit.OpCodes.Ldarg_0 on line var jpegDecoded = decoder.Decode();<br /><br />I can't work out why. Any ideas?Anonymousnoreply@blogger.com