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 : 13784
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

{Visual Basic, VB6, VB.NET, VB 2005} MERGEIMAGES
Assembler 2 images pour n'en faire qu'une. Vous ouvrez 2 images de même hauteur en pixel et de même ...

{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} 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} ACTION_MENU_CONTEXTUEL_FICHIER
Associer un programme (pas l'exe) au menu contextuel (clic droit) d'un fichier. J'ai mis 2 exemples...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTIR_COMPRESSER_TIF
Convertir et compresser des fichiers JPEG et BMP en TIF. Image par image ou par lot.Vous pouvez voir...

{Visual Basic, VB6, VB.NET, VB 2005} CREER_MULTIPAGE_TIF
Créer un fichier multipage Tif de 2 ou 3 images, en VB.Net et VBA Excel. Avec Microsoft Windows Imag...

{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 plusieurs i...

{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, ...