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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
namespace WpfApplication1
{
/// <summary>
/// Logique d'interaction pour Window6.xaml
/// </summary>
public partial class Window6 : Window
{
public Window6()
{
InitializeComponent();
}
private void btnCreateWBIndexed_Click(object sender, RoutedEventArgs e)
{
PixelFormat pf = PixelFormats.Indexed2 ;
int width = 128;
int height = width;
int stride = (width * pf.BitsPerPixel + 7) / 8;
byte[] pixels = new byte[height * stride];
// Try creating a new image with a custom palette.
IList <Color> colors = new List<Color>();
colors.Add(Colors.White );
colors.Add(Colors.Magenta );
colors.Add(Colors.Red);
colors.Add(Colors.LimeGreen );
BitmapPalette myPalette = new BitmapPalette(colors);
//on remplit les pixels
Random rndPixels = new Random();
rndPixels.NextBytes(pixels);
// Creates a new empty bitmpasource with the pre-defined palette
BitmapSource mybmp = BitmapSource.Create(
width,
height,
96,
96,
pf,
myPalette ,
pixels,
stride);
img1.Source = mybmp ;
// Creates a new empty writeablebitmap with the pre-defined palette
WriteableBitmap wb = new WriteableBitmap(width,
height,
96,
96,
pf,
myPalette
);
wb.WritePixels(new Int32Rect(0, 0, width, height), pixels, stride, 0);
img2.Source = wb;
}
}
} |
Partager