Bonjour,

je souhaite construire un objet ImageDrawing basé sur un bitmap stocké sur le disque. Jusqu'à présent, tout allait bien :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
ImageSourceConverter conv = new ImageSourceConverter();
 
new ImageDrawing(
                    (ImageSource)conv.ConvertFromString(cheminDuBitmap),
                    new System.Windows.Rect(
                        0,
                        0,
                        Largeur,
                        Hauteur
                        )));
A présent, je souhaite réaliser la même chose à partir d'une image stockée dans les 'Resources' de l'assembly :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
Bitmap bmp = Affichage.Properties.Resources.punaise_centre;
 
 System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
                     System.Drawing.Imaging.BitmapData bmpData =
                         bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                         bmp.PixelFormat);
 
                     // Get the address of the first line.
                     IntPtr ptr = bmpData.Scan0;
 
                     // Declare an array to hold the bytes of the bitmap.
                     int bytes = bmpData.Stride * bmp.Height;
                     byte[] rgbValues = new byte[bytes];
 
                     // Copy the RGB values into the array.
                     System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
 
                    PixelFormat pf = PixelFormats.Bgr32;
 
                    int rawStride = (bmp.Width * pf.BitsPerPixel + 7) / 8;
 
                     //// Create a BitmapSource.
                     BitmapSource bitmap = BitmapSource.Create(bmp.Width, bmp.Height,
                         96, 96, pf, null,
                         rgbValues, rawStride);
 
                        ImageDrawing ig = new ImageDrawing(
                            (ImageSource)bitmap,
                             new System.Windows.Rect(
                                pixW,
                                pixH,
                                size.Width,
                                size.Height
                                ));
Quelqu'un a-t-il une autre idée plus simple ? J'ai essayé pas mal de chose mais je n'ai trouvé aucune compatibilité entre BitmapImage et Bitmap.
Et sinon, le gros problème est que je perds la couleur de transparence de l'image d'origine !
Une solution pour ça ??