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

Windows Presentation Foundation Discussion :

Conversion ImageSource en Bitmap


Sujet :

Windows Presentation Foundation

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2015
    Messages : 7
    Points : 6
    Points
    6
    Par défaut Conversion ImageSource en Bitmap
    Bonjour a tous ,
    me voilà confronter à un problème sur lequel je ne trouve pas de réponse
    vous êtes mon dernier espoir ahah .
    plus sérieusement j'ai une application wpf dans laquelle ce trouve une vue avec un control Image.
    j'ai un objet webcam qui vient actualiser les frames prise par la caméra dans ce control jusque la rien de sorcier.
    quand je souhaite récuperer une image a un instant t je la recupère en faisant image.source qui me retourne un objet ImageSource.
    cette image récupérée j'ai besoin de la modifier pour la rogner donc je voudrais la caster en Bitmap et la c'est le drame j'ai tout tester dans tous les sens depuis 2 jours et je ne trouve vraiment pas de solution ca me rend fou ^^.

    mon code pour le moment
    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
     
     
     
     
     Bitmap testbitm = new Bitmap((ImageBordure.Source as BitmapImage)); // c'est ici que ca va pas 
                    CroppedBitmap cb = new CroppedBitmap(Convertbs(testbitm), new Int32Rect(20, 20, 100, 100));
                    // Set Image.Source to cropped image  
     
                    _miniatureImage[pointintab].Source = cb.Source;
     
     
       public BitmapSource Convertbs(System.Drawing.Bitmap bitmap)
            {
                var bitmapData = bitmap.LockBits(
                    new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
     
                var bitmapSource = BitmapSource.Create(
                    bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr24, null,
                    bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
     
                bitmap.UnlockBits(bitmapData);
                return bitmapSource;
            }
    j'ai vraiment tout tenté alors si vous avez déja ete confronter a ce problème ou si vous avez la solution je vous serais vraiment reconnaissant de me la faire partager merci .

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour
    Il y a confusion manifeste entre WPF et les Winforms ...
    System.Drawing.Imaging est un espace de nom inconnu dans Wpf
    Remplacé par System.Windows.Media.Imaging....
    idem pour le class Bitmap remlace par BitmapImage ...
    La manip d'images est plus simple en WPF...
    ton code revu :
    1/xaml 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
     
     
    <Window x:Class="WpfBitmapCropped.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <Image Name="img1" Width="200" Source="Images/Koala.jpg" Stretch="Fill"/>
            <Image Name="img2" Width="200"/>
            <Button Name="btnCrop" Content="Crop" Click="btnCrop_Click"></Button>
            <TextBlock Name="myTextBlock"/>
        </StackPanel>
    </Window>
    2/code .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
     
     
     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.Navigation;
    using System.Windows.Shapes;
    using System.IO;
     
    namespace WpfBitmapCropped
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
     
            private void btnCrop_Click(object sender, RoutedEventArgs e)
            {
     
                BitmapSource  testbitm = this.img1.Source  as BitmapSource; // c'est ici que ca va pas 
     
                CroppedBitmap cb =new CroppedBitmap(testbitm, new Int32Rect(400, 400, 100, 100)); 
     
                // Set Image.Source to cropped image  
     
                  this.img2.Source = cb;
     
     
                  // Pour sauvegarder ce  CroppedBitmap
                FileStream stream = new FileStream(Directory.GetCurrentDirectory()+ "/Images/empty.png", FileMode.Create);
                PngBitmapEncoder  encoder = new  PngBitmapEncoder();
     
                this.myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
                encoder.Frames.Add(BitmapFrame.Create(cb));
                encoder.Save(stream);
     
     
            }
                //------------------------------CODE A VIRER-------------------------------//
            // public BitmapSource Convertbs( BitmapImage bitmap)
            //{
                //var bitmapData = bitmap.LockBits(
                //    new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                //    System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
     
                //var bitmapSource = BitmapSource.Create(
                //    bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr24, null,
                //    bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
     
                //bitmap.UnlockBits(bitmapData);
                //return bitmapSource;
            //}
        }
    }
    bon code...

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2015
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    wouha je m'attendais pas à une réponse aussi claire et détaillée.
    je me doutais bien que c'etait pas grand chose.
    j'ai bien compris ou etait le problème du coup .

    Merci beaucoup pour cette réponse je peux enfin m'y remettre tu m'enlèves une épine du pied .

  4. #4
    Membre averti
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Novembre 2014
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2014
    Messages : 196
    Points : 331
    Points
    331
    Par défaut
    Du coup le post est résolu ?

Discussions similaires

  1. Réponses: 2
    Dernier message: 08/10/2002, 18h26
  2. transfert d'un fichier bitmap en socket tcp
    Par localhost dans le forum C++Builder
    Réponses: 5
    Dernier message: 29/07/2002, 00h40
  3. Pb Lecture de bitmap monochrome
    Par Loïc38 dans le forum C++Builder
    Réponses: 4
    Dernier message: 02/07/2002, 18h24
  4. Lecture d'une image bitmap
    Par Geronimo dans le forum x86 32-bits / 64-bits
    Réponses: 18
    Dernier message: 28/06/2002, 12h01
  5. Comment faire pour créer un bitmap
    Par GliGli dans le forum C++Builder
    Réponses: 2
    Dernier message: 24/04/2002, 15h41

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