Les Snippets

Connexion

Transformer l'image d'une PictureBox en un tableau de bytes

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 21/11/2007 23:51:20 et initié par Willi [Liste]
Vue : 8468
Catégorie(s) : Graphique
Langages dispo pour ce code :
- VB 2005
- C# 2.x
- Delphi 5
- Delphi 5
- VB6



Langage : VB 2005
Date ajout : 21/11/2007
Posté par Willi [Liste]
Public Function PictureBoxImageToBytes(ByVal picBox As PictureBox) As Byte() 
If (picBox IsNot Nothing) AndAlso (picBox.Image IsNot Nothing) Then
Dim bmp As New Bitmap(picBox.Image)Dim ms As New System.IO.MemoryStream() 
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim buff As Byte() = ms.ToArray() 
ms.Close()

ms.Dispose()
Return buff 
Else

Return Nothing

End If

End Function

Remarque :
Utilisation:
Dim picBytes as byte()=PictureBoxImageToBytes(VotrePictureBox)
Langage : C# 2.x
Date ajout : 21/11/2007
Posté par Willi [Liste]
public byte[] PictureBoxImageToBytes(PictureBox picBox) 
{
     if ((picBox != null) && (picBox.Image != null))
    {
         Bitmap bmp = new Bitmap(picBox.Image);
         System.IO.MemoryStream ms = new System.IO.MemoryStream();

         bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
           
           byte[] buff = ms.ToArray();

         ms.Close();
         ms.Dispose();
         return buff;
    }
     else
    {
         return null;
    }
}

Remarque :
Utilisation:
byte[] picBuff = PictureBoxImageToBytes(VotrePictureBox);
Langage : Delphi 5
Date ajout : 05/07/2008
Posté par f0xi [Liste]
type
  TBytesDynArray = array of byte;
procedure BitmapToBytesArray(Bitmap: TBitmap; var BytesArray: TBytesDynArray);
var pX : ^byte;
    N,LZ : integer;
begin
  case Bitmap.PixelFormat of
    pf8bit : LZ := (Bitmap.Width * Bitmap.Height);
    pf16bit: LZ := (Bitmap.Width * Bitmap.Height) * 2;
    pf24bit: LZ := (Bitmap.Width * Bitmap.Height) * 3;
    pf32bit: LZ := (Bitmap.Width * Bitmap.Height) * 4;
  end;
  pX := Bitmap.ScanLine[Bitmap.Height-1];
  SetLength(BytesArray, LZ);
  CopyMemory(@BytesArray[0], pX, LZ);
end;

exemple :
var
  BA : TBytesArray;
begin
  BitmapToBytesArray(ImageX.Picture.Bitmap, BA);
  ...
end;

Langage : Delphi 5
Date ajout : 16/07/2008
Posté par cirec [Liste]
Type
  TBArray = Array of Byte;
Var aBArray : TBArray;
procedure Bitmap2ByteArray(const aBMP: TBitmap;var ByteArray: TBArray);
var MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    aBMP.SaveToStream(MS);
    MS.Seek(0, soFromBeginning);
    SetLength(ByteArray, MS.size);
    MS.ReadBuffer(ByteArray[0], Length(ByteArray));
  finally
    MS.free;
  end;
end;

// Utilisation
procedure TForm1.Button1Click(Sender: TObject);
begin
  Bitmap2ByteArray(Image1.Picture.Bitmap, aBArray);
end;

Remarque :
la différence ici est que ce code prend en charge le bitmap complet (la taille la palette le PixelFormat etc.) pas seulement la série de Pixels
Langage : VB6
Date ajout : 04/07/2009
Posté par PCPT [Liste]
Private Type BITMAP
    bmType       As Long
    bmWidth      As Long
    bmHeight     As Long
    bmWidthBytes As Long
    bmPlanes     As Integer
    bmBitsPixel  As Integer
    bmBits       As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As LongByVal nCount As Long, lpObject As Any) As  Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As LongByVal dwCount As Long, lpBits As Any) As  Long
Function PictureToArray(ByRef img As IPicture) As Byte()
    Dim tBM     As BITMAP
    Dim abRet() As Byte
    Dim lSize   As Long
'    structure
    Call GetObject(img.handle, Len(tBM), tBM)
'   préparation du tableau
    lSize = (tBM.bmWidth * 3And &HFFFFFFFC
    ReDim abRet(To lSize * tBM.bmHeight * 3)
'   attribution,  retour
    Call GetBitmapBits(img.handle, UBound(abRet), abRet(0))
    PictureToArray = abRet
    Erase abRet
End Function
Remarque :
Dim a() As Byte
a = PictureToArray(Picture1.Image)

Snippets en rapport avec : Byte, Image, Picturebox



Codes sources en rapport avec : Byte, Image, Picturebox

{Visual Basic, VB6, VB.NET, VB 2005} RECADRER_IMAGE
Recadrer une image en choisissant la dimension de sortie du plus grand côté en cm. Un commentaire a...

{Visual Basic, VB6, VB.NET, VB 2005} MODIFIER_RÉSOLUTION_PPP_IMAGE
Modifier la résolution ppp d'une image par lot ou image par image. Les dimensions des côtés de l'ima...

{Visual Basic, VB6, VB.NET, VB 2005} RÉORGANISER UNE LISTVIEW AVEC DES IMAGES DANS LES 3 PREMIÈRES COLONES
Cette source est la première que je dépose donc j'attends vos commentaires. Mon but était de réussi...

{Visual Basic, VB6, VB.NET, VB 2005} DRAG AND DROP IMAGE INTERNET
J'ai eu du mal a le faire donc je le met car cela peut etre utile pour mettre des images dans une ba...

{Visual Basic, VB6, VB.NET, VB 2005} REDIMENSION D'IMAGE
Voici un petit logiciel simple de redimension d'image : - en gardant les proportions ou non - en c...

{Visual Basic, VB6, VB.NET, VB 2005} AFFICHE ET ENREGISTRE UNE IMAGE D'UNE WEBCAM (SNAPSHOT) À FRÉQUENCE DÉSIRÉE
En quelques lignes, ce code va d'abord chercher l'image dans la WEBCam puis l'enregistre sur le DD a...

{Visual Basic, VB6, VB.NET, VB 2005} PICTUREBOX "INTELLIGENT" (DU MOINS PLUS QUE CELUI LIVRÉ EN STANDARD :)
Ce control utilisateur est une picture box qui permet d'afficher une image en la redimensionnant afi...

{C# / C#.NET} ZOOM D'UNE IMAGE AVEC L'AFFICHAGE DE SCROOLBARS LE PLUS SIMPLE POSSIBLE
Salut Sur une demande de Moucave dans le forum, voila un tit prog pour zoomer, pour les scrool bar...

{Visual Basic, VB6, VB.NET, VB 2005} AFFICHER ET MODIFIER DES IMAGES DANS UN DATAGRID
Un composant qui permet de visualiser des images dans une colonne de DataGrid. Le composant permet é...

{JAVA / J2EE} TÉLÉCHARGEMENT D'IMAGES (POCHETTES CD, DVD, LIVRES...) SUR INTERNET
Petite fonction permettant de télécharger des images de cds, bd, livres, dvd, films, affiches par r...