Les Snippets

Connexion

Convertir un tableau de bytes en image

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 05/11/2007 11:30:28 et initié par Willi [Liste]
Date de mise à jour : 05/11/2007 11:32:16
Vue : 9046
Catégorie(s) : Graphique
Langages dispo pour ce code :
- C# 2.x
- VB 2005
- Delphi 5
- Delphi 5
- VB6



Langage : C# 2.x
Date ajout : 05/11/2007
Posté par Willi [Liste]
DateMAJ : 05/11/2007
public static Image StreamToImage(byte[] buff) 
{
MemoryStream ms = new MemoryStream(buff); 
Image img = Image.FromStream(ms);
return img; 
}


Langage : VB 2005
Date ajout : 05/11/2007
Posté par Willi [Liste]

Public Shared Function StreamToImage(ByVal buff As Byte()) As Image

    Dim ms As New MemoryStream(buff)

    Dim img As Image = Image.FromStream(ms)
    Return img

End Function

Langage : Delphi 5
Date ajout : 05/07/2008
Posté par f0xi [Liste]
procedure BytesArrayToBitmap(const BytesArray: array of byte; const W, H: integer;
                             const PixelFormat: TPixelFormat; Bitmap: TBitmap);
var TBmp: TBitmap;
    pX : ^byte;
    LZ : integer;
begin
  TBmp := TBitmap.Create;
  try
    case 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;
    TBmp.PixelFormat := PixelFormat;
    TBmp.Width := W;
    TBmp.Height:= H;
    pX := TBmp.ScanLine[TBmp.Height-1];
    CopyMemory(pX, @BytesArray[0], LZ);
    Bitmap.Assign(TBmp);
  finally
    TBmp.Free;
  end;
end;
Langage : Delphi 5
Date ajout : 16/07/2008
Posté par cirec [Liste]

procedure ByteArray2Bitmap(const aBMP: TBitmap;const ByteArray: Array of Byte);
var MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    MS.WriteBuffer(ByteArray[0], Length(ByteArray));
    MS.Seek(0, soFromBeginning);
    aBMP.LoadFromStream(MS);
  finally
    MS.free;
  end;
end;
// Utilisation
procedure TForm1.Button2Click(Sender: TObject);
begin
  ByteArray2Bitmap(Image2.Picture.Bitmap, aBArray);
end;

Remarque :
contrairement au code de f0xi celui-ci n'a pas besoin de connaitre ni la taille ni le PixeFormat du Bitmap pour l'afficher ... tout se trouve dans le tableau de bytes qui a été crée avec ce code :
http://www.codyx.org/snippet_transformer-image-picturebox-tableau-bytes_496.aspx#1953
Langage : VB6
Date ajout : 04/07/2009
Posté par PCPT [Liste]
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As LongByVal dwCount As Long, lpBits As Any) As  Long
Sub ArrayToPicture(ByRef byteArray() As Byte, ByRef imgDest As IPicture)
    Call SetBitmapBits(imgDest.handle, UBound(byteArray),  byteArray(0))
End Sub
Remarque :
ArrayToPicture a, Picture2.Image
Picture2.Refresh

NB : le tableau de bytes 'a' doit être valide, voir la fonction inverse correspondante :
http://www.codyx.org/snippet_transformer-image-picturebox-tableau-bytes_496.aspx#2389

Snippets en rapport avec : Image, Convertir, Bitmap, Bytes



Codes sources en rapport avec : Image, Convertir, Bitmap, Bytes

{C / C++ / C++.NET} ID3 TAG COVER ALBUM IMAGE
album art cover ajout pour mp3 choisi un dossier avec de la music mp3 si il y a un ou plusieur im...

{Visual Basic, VB6, VB.NET, VB 2005} DÉCOUPEUR DE SPRITES (POUR JEUX RPG)
Voici une petite source toute simple qui permet de découper des sprites de manière industrielle, eff...

{Delphi} ANAGLYPHEUR OU COMMENT VOIR EN RELIEF LES STÉRÉOSCOPES ANCESTRAUX ...
Permet de créer un anaglyphe affichable en plein écran à partir d'images stéréoscopiques (liées ou n...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTIR FORMAT IMAGE
Convertir les formats image par lot ou image par image. Formats supportés: jpg, gif, bmp, wmf, png, ...

{Visual Basic, VB6, VB.NET, VB 2005} MENU POPUP AVEC IMAGE
Placer des bitmaps 16*16 pixel dans les menus standards et popup des forms. J'ai essayé d'utilis...

{Delphi} UNITE GRAPHIQUE IMOD, AVEC EXEMPLE
Bonjour, Voici une unité graphique, reprenant la plupart des opérations et des manipulations sur ...

{Delphi} MINIMISER LES COULEURS D' UNE IMAGE VENANT DU SCANNER OU AUTRE
Dans le même esprit que ma source : http://www.delphifr.com/codes/CHANGER-COULEUR-PIXEL-PIXELS-COUL...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTIR LES IMAGESDE CD/DVD ALCOHOL 120 (.MDF), BLINDWRITE (.BXI) , DISCJUGGLER (.CDI) EN IMAGE DE CD/DVD .ISO
un petit composant ocx que je viens de coder et qui permet de convertir les images de CD/DVD Alco...

{C / C++ / C++.NET} BOITE DE DIALOGUE ANIMÉE AVEC IMAGE DE FOND
Ceci est mon premier code. Je pense que si on est resté longtemps consommateur, on peut en retour d...

{Visual Basic, VB6, VB.NET, VB 2005} OUVERTURE D'UN FICHIER BITMAP EN NATIF
Ce code permet d'ouvir un fichier Bmp. Oui, c'est sur il y a le composant Microsoft qui fait mieu...