IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

WriteableBitmap + Indexed8


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2006
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Novembre 2006
    Messages : 82
    Par défaut WriteableBitmap + Indexed8
    Bonjour,

    Je m'arrache les cheveux (pour changer) pour initialiser un bête WriteableBitmap :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    List<Color> col = new List<Color>();
    col.Add(Colors.Black);
    col.Add(Colors.White);
    BitmapPalette new_pal = new BitmapPalette((IList<Color>) col);
     
    WriteableBitmap wb = new WriteableBitmap(20, 20, 96, 96, PixelFormats.Bgra32, null);
    try
    {
        wb = new WriteableBitmap(20, 20, 96, 96, PixelFormats.Indexed1, new_pal);
    }
    catch (Exception e)
    {
       Console.WriteLine(e.Message);
    }
    L'initialisation avec le format BGRA marche nickel, mais dès que j'essaie d'initialiser en Indexed, ça bloque :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    InvalidCastException à System.Windows.Media.Imaging.WriteableBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat, BitmapPalette palette)
    Merci d'avance pour votre aide !

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour

    Tu as des problemes avec la "ligne de numerisation" (stride) qui depend du pixelformat utiise..
    De plus la "ligne de numerisation" gouverne les dimensions de l'arraybyte de pixels...

    voici un exemple qui cree un BitmapSource et un WriteableBitmap avec:
    - un PixelFormats.Index2 ( palette de 4 couleurs )
    - remplits les 2 bitmaps de pixels aleatoires et les affiches...

    Ne t'arrache pas la queue de cheval (coupe-la pour plus de surete...!!!)
    code xaml du simple winform..
    Code xaml : 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
    <Window x:Class="WpfApplication1.Window6"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window6" Height="300" Width="300">
        <Grid  >
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Button x:Name="btnCreateWBIndexed" Content="CreateWB" Click="btnCreateWBIndexed_Click"/>
            <Image
                Grid.Row="1"
                x:Name="img1"
                Width="200"
                Height="100">
            </Image>
            <Image 
                Grid.Row="2"
                x:Name="img2"
               Width="200"
                Height="100">
            </Image>
        </Grid>
    </Window>

    code behind.cs du form:
    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
    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;
            }
        }
    }
    bon code..........

  3. #3
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juillet 2014
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Juillet 2014
    Messages : 10
    Par défaut
    bonjour !

    J'ai une petite question faisant suite à la réponse de MABROUKI.

    Si on a une image en entrée et qu'on désire balayer les pixels (array de bytes) on doit donner un stride qui sera fonction du PixelFormat. Ce PixelFormat dépend lui de l'image en entrée. Comment faire donc pour calculer ce stride quelque soit l'image en entrée et donc son PixelFormat ?

    Faut-il faire par exemple un Switch avec tous les PixelFormats existants et en déduire le Stride ?

    merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Windows Phone 7] Rendu d'u TextBlock vers une WriteableBitmap
    Par toss.net dans le forum Windows Phone
    Réponses: 14
    Dernier message: 24/09/2013, 18h02
  2. [WPF] WriteableBitmap & System.Drawing.Bitmap
    Par NeoKript dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 20/11/2011, 06h00
  3. Empecher l'interpolation de WriteableBitmap
    Par Harry_polin dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 05/09/2011, 22h00
  4. comment s'assurer du rendu complet d'un WriteableBitmap
    Par filobilo dans le forum Silverlight
    Réponses: 5
    Dernier message: 15/02/2011, 12h16
  5. Fonctions introuvable de WriteableBitmap en .net 3.5
    Par Harry_polin dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 18/12/2009, 09h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo