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 : 13537
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} TRIEUR DE PHOTOS
Ce programme permet de sélectionner des photos et de les enregistrer dans un dossier de destination....

{Visual Basic, VB6, VB.NET, VB 2005} LIST DES IMAGES AVEC BASE DE DONNÉES ET REPERTOIR D'IMAGE
Généralement il n'est pas conseillé de stock directement des images dans la base de donnée. Il fau...

{Visual Basic, VB6, VB.NET, VB 2005} CREER UN GIF ANIMÉ
Ce programme crée un gif animé grâce à la librairie Gif.Components.DLL. Son utilisation est très si...

{Visual Basic, VB6, VB.NET, VB 2005} IMAGELOARDER
ce petit code permet de charger une image.il montre la différence entre PictureBox et Image. ça cha...

{Visual Basic, VB6, VB.NET, VB 2005} CREER UN CALENDRIER DE POCHE
Création d'un calendrier pliable pour la nouvelle année. Le code est en VB2008 et utilise Word pour ...

{Visual Basic, VB6, VB.NET, VB 2005} MODIFIER COULEUR IMAGE PAR LOT
Modifier la couleur des images par lot ou image par image. La modification se fait à l'aide de la co...

{Visual Basic, VB6, VB.NET, VB 2005} COLORMATRIX_VB
Pour mieux comprendre le fonctionnement de la Colormatrix. En cliquant sur l'image source vous ouvr...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTIR IMAGE EN TON SÉPIA
Convertir une image couleur en ton sépia. A l'ouverture de l'application vous choisissez votre image...

{Visual Basic, VB6, VB.NET, VB 2005} REDIMENSIONNEMENT PROPORTIONNEL D'UNE IMAGE (PICTUREBOX)
Cette fonction est l'adaptation d'une fonction écrite en C# (dont je ne me souviens plus de la sourc...

{Visual Basic, VB6, VB.NET, VB 2005} DIAPORAMA_DRAG_AND_DROP
L'originalité de cette visionneuse d'images est dans le défilement manuel. Par cliquer-glisser du c...