3

I have found how to do this in .NET 4.0, but I think JpegBitmapEncoder doesn't exist in Silverlight:

MemoryStream memStream = new MemoryStream();              
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageC));
encoder.Save(memStream);
var bytes = memStream.GetBuffer();

How can I convert an image to bytes[] in silverlight?

UPDATE:

I have a Contact model, which has a Photo property. Whenever I add a new Contact, I would like to load a local default Image and convert it and set the Photo property to it.

var bitmapImage = new BitmapImage
                            {
                                UriSource = new Uri("pack://application:,,,/xxx;component/Images/default.JPG")
                            };
            var image = new Image{Source = bitmapImage};

Is this the correct way to load an image in first place?

Houman
  • 64,245
  • 87
  • 278
  • 460

2 Answers2

5

Use

myImage.Save(memStream, ImageFormat.Jpeg);
return memStream.ToArray();

UPDATE

OK it turns out that the image is a BitmapImage.

It seems that BitmapImage does not expose the functionality to save the image. The solution is to get the image from the embedded resource:

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Please see my update, I don't see the Save() method in image, unless I am doing something wrong here. – Houman Mar 18 '11 at 12:33
  • Well, as I posted it above it is a .JPG – Houman Mar 18 '11 at 12:48
  • Sorry I don't understand how your solution should work. Your Image is from System.Drawing.Image namespace. From my understanding you don't even have that in Silverlight. – Houman Mar 18 '11 at 13:00
  • Try this solution I posted. Use the path not in the silverlight fromat but in the namespace format. – Aliostad Mar 18 '11 at 13:06
  • I tried this without success: Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("../Images/default.JPG"); If my URi to the picture is like this, what should I be putting in there? "pack://application:,,,/xxx;component/Images/default.JPG" Th erelative path didnt work :( – Houman Mar 18 '11 at 14:11
  • No you need to see what URL it has within the assembly. Normally it is `.Images.default.JPG` so paths are joined by dot and not slash. – Aliostad Mar 18 '11 at 14:21
  • Have a look here: http://stackoverflow.com/questions/27757/how-can-i-discover-the-path-of-an-embedded-resource – Aliostad Mar 18 '11 at 14:22
  • I feel now quite stupid. But I dont get it work: Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("SalesContactManagement.Modules.NavigationModule.Images.default.JPG"); I am expecting to see the path when I type it directly in code: SalesContactManagement.Modules.NavigationModule.Images The Images can't be found. I have set the default image to Embedded Resource and tested it also as Resource. No luck... – Houman Mar 18 '11 at 14:31
  • Use `Assembly.GetExecutingAssembly().GetManifestResourceNames()` to see all the names and then copy the path you need to use. – Aliostad Mar 18 '11 at 14:35
0

Have a look at this library: Imagetools

It contains some nice utilities and jpg and png encoders,

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Erno, neat solutionm but even using an ExtendedImage from that library, how does it help me to convert the image into byte[] ? – Houman Mar 18 '11 at 13:55
  • This encoders are indeed essential. I got it finally working. – Houman Apr 02 '11 at 08:13