Extrait une image d'un film pour l'afficher dans une picturebox

astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005 - 13 juin 2005 à 14:08
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 - 28 juin 2005 à 02:04
Bonjour,

Voila j'essaie
d'extraire une image d'un films pour ensuite faire du traitement
d'image dessus, mais j'aimerai afficher image par image dans ma
picturebox.

J'utilise C#

J'ai une classe qui s'appelle
AviReader, qui me permet d'ouvrir mon film et faire ce que je ve avec,
et voici mon code qui devrai normalement extraire l'image à la position
"position" (paramétre d'entre de lafonction) et cet fonction doit me
retourner un object de type Bitmap pour l'afficher dans ma picturebox.



public Bitmap ExtraitBmpAvi(int position)

{

if(position > countFrames)

{

throw new Exception("Invalid frame position");

}



//Decompress the frame and return a pointer to the DIB

int pDib =
Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

//Copy the bitmap header into a managed struct

Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();

bih =
(Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib),
bih.GetType());



/*if(bih.biBitCount < 24){


throw new Exception("Not enough colors! DIB color
depth is less than 24 bit.");

}else */

if(bih.biSizeImage < 1)

{


throw new Exception("Exception in AVIStreamGetFrame:
Not bitmap decompressed.");

}



//Copy the image

byte[] bitmapData = new byte[bih.biSizeImage];

int address = pDib + Marshal.SizeOf(bih);

for(int offset=0; offset<bitmapData.Length; offset++)

{


bitmapData[offset] = Marshal.ReadByte(new
IntPtr(address));

address++;

}

MemoryStream mm = new MemoryStream(bitmapData);

Bitmap bmp = new Bitmap(Image.FromStream(mm));

mm.Close();

return bmp;

}



Alors tout simplement ca plante, je pense que c'est au niveau de la mémoire, mais la, j'en ai pas la moindre idée.

merci pour toute l'aide que vous pouvez m'apporter

@pluche

@str@goth

35 réponses

sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 14:42
Quelle est l'erreur que tu obtiens ?

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 14:45
voila mon erreur:



An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll



Additional information: Object reference not set to an instance of an object.
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 14:47
ok... donc pour moi, cette ligne :
Bitmap bmp = new Bitmap(Image.FromStream(mm));

doit-te retourner un bitmap null.

As-tu essayé t'exécuter ta méthode en Pas à pas ?

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 14:51
euh, non,

mais je devrai voir koi ???
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 14:57
tu verras où le code plante ;)
c'est plus simple pour débugger non ?

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 14:58
voila mon erreur en pas a pas



See the end of this message for details on invoking

just-in-time (JIT) debugging instead of this dialog box.



************** Exception Text **************

System.ArgumentException: Invalid parameter used.

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)

at System.Drawing.Image.FromStream(Stream stream)

at PictureKey.AviReader.ExtraitBmpAvi(Int32 position) in d:\banyuls\essai\develop\avireader.cs:line 203

at develop.Form1.openFileDialog1_FileOk(Object sender,
CancelEventArgs e) in d:\banyuls\essai\develop\form1.cs:line 243

at System.Windows.Forms.FileDialog.OnFileOk(CancelEventArgs e)

at System.Windows.Forms.FileDialog.DoFileOk(IntPtr lpOFN)





************** Loaded Assemblies **************

mscorlib

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll

----------------------------------------

develop

Assembly Version: 1.0.1990.24893

Win32 Version: 1.0.1990.24893

CodeBase: file:///D:/Banyuls/essai/develop/bin/Debug/develop.exe

----------------------------------------

System.Windows.Forms

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase:
file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll

----------------------------------------

System

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll

----------------------------------------

System.Drawing

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll

----------------------------------------

Accessibility

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.573

CodeBase: file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll

----------------------------------------



************** JIT Debugging **************

To enable just in time (JIT) debugging, the config file for this

application or machine (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.



For example:



<configuration>

<system.windows.forms jitDebugging="true" />

</configuration>



When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the machine

rather than being handled by this dialog.





et c bien sur la ligne que tu m'a dit, mais as ton avis, je doit faire koi ???
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 15:10
bizarre... mon message n'a pas était pris en compte

donc :
ta ligne : Bitmap bmp = new Bitmap(Image.FromStream(mm));
est fausse...

il faut la remplacer par : Bitmap bmp = (Bitmap)Image.FromStream(mm);

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 15:20
See the end of this message for details on invoking

just-in-time (JIT) debugging instead of this dialog box.



************** Exception Text **************

System.ArgumentException: Invalid parameter used.

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)

at System.Drawing.Image.FromStream(Stream stream)

at PictureKey.AviReader.ExtraitBmpAvi(Int32 position) in d:\banyuls\essai\develop\avireader.cs:line 204

at develop.Form1.openFileDialog1_FileOk(Object sender,
CancelEventArgs e) in d:\banyuls\essai\develop\form1.cs:line 243

at System.Windows.Forms.FileDialog.OnFileOk(CancelEventArgs e)

at System.Windows.Forms.FileDialog.DoFileOk(IntPtr lpOFN)





************** Loaded Assemblies **************

mscorlib

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll

----------------------------------------

develop

Assembly Version: 1.0.1990.25728

Win32 Version: 1.0.1990.25728

CodeBase: file:///D:/Banyuls/essai/develop/bin/Debug/develop.exe

----------------------------------------

System.Windows.Forms

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase:
file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll

----------------------------------------

System

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll

----------------------------------------

System.Drawing

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.2032

CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll

----------------------------------------

Accessibility

Assembly Version: 1.0.5000.0

Win32 Version: 1.1.4322.573

CodeBase: file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll

----------------------------------------



************** JIT Debugging **************

To enable just in time (JIT) debugging, the config file for this

application or machine (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.



For example:



<configuration>

<system.windows.forms jitDebugging="true" />

</configuration>



When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the machine

rather than being handled by this dialog.





voila l'erreur k'il me met avec ta correction,

exécution pas à pas !!!
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 15:34
ok... je pense que ton champ de byte n'est pas correct.
l'erreur doit se situer la dedans :

byte[] bitmapData = new byte[bih.biSizeImage];
int address = pDib + Marshal.SizeOf(bih);
for(int offset=0; offsetSébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 15:56
voila le programme ki ma servit de base pour faire le mien, celui ci
permet d'extraire une image d'un film, et de l'enregistrer sur le diske
dur directement...

et ca ca marche nikel













public void ExportBitmap(int position, String dstFileName){

if(position > countFrames){

throw new Exception("Invalid frame position");

}



//Decompress the frame and return a pointer to the DIB

int pDib =
Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

//Copy the bitmap header into a managed struct

Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();

bih =
(Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib),
bih.GetType());



/*if(bih.biBitCount < 24){


throw new Exception("Not enough colors! DIB color
depth is less than 24 bit.");

}else */

if(bih.biSizeImage < 1){


throw new Exception("Exception in AVIStreamGetFrame:
Not bitmap decompressed.");

}



//Copy the image

byte[] bitmapData = new byte[bih.biSizeImage];

int address = pDib + Marshal.SizeOf(bih);

for(int offset=0; offset //Create file header

Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();

bfh.bfType = Avi.BMP_MAGIC_COOKIE;

bfh.bfSize =
(Int32)(55 + bih.biSizeImage); //size of file as written to disk

bfh.bfReserved1 = 0;

bfh.bfReserved2 = 0;

bfh.bfOffBits = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);



//Create or overwrite the destination file

FileStream fs = new FileStream(dstFileName, System.IO.FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);



//Write header

bw.Write(bfh.bfType);

bw.Write(bfh.bfSize);

bw.Write(bfh.bfReserved1);

bw.Write(bfh.bfReserved2);

bw.Write(bfh.bfOffBits);

//Write bitmap info

bw.Write(bitmapInfo);

//Write bitmap data

bw.Write(bitmapData);

bw.Close();

fs.Close();

}



ca qui est en gras c'est ce que j'ai enlever, qui ne me sert pas pour mon programme

voila, si ca peut t'aider pour m'aider, ca pourrai etre cool
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 16:07
ok, je comprends mieux.
en fait, ce que tu fais toi, c'est que tu ne copies que les Datas de ton image dans le MemoryStream.
Malheureusement, ce que tu as oublié, c'est qu'une image contient aussi une entête.

il faut donc que tu écrives la même chose dans ton memoryStream.

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 16:18
Ok,

alors si je comprend bien, il faut que je concaténe ma stream Info et ma Stream Data ???

et je dois faire ca comment ?
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 16:34
Voila ce que j'ai fait, mais ca merde toujours, dis moi, si tu trouve mon prob merci



public Bitmap ExtraitBmpAvi(int position)

{

if(position > countFrames)

{

throw new Exception("Invalid frame position");

}



//Decompress the frame and return a pointer to the DIB

int pDib =
Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

//Copy the bitmap header into a managed struct

Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();

bih =
(Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib),
bih.GetType());



/*if(bih.biBitCount < 24){


throw new Exception("Not enough colors! DIB color
depth is less than 24 bit.");

}else */

if(bih.biSizeImage < 1)

{


throw new Exception("Exception in AVIStreamGetFrame:
Not bitmap decompressed.");

}



//Copy the image

byte[] bitmapData = new byte[bih.biSizeImage];

int address = pDib + Marshal.SizeOf(bih);

for(int offset=0; offset //Bitmap ...

long nbelem = Marshal.SizeOf(bih) + (long) bih.biSizeImage;

byte[] bitmap = new byte[(int)nbelem];

for(int offset=0; offset

MemoryStream mm = new MemoryStream(bitmap);

Bitmap bmp = (Bitmap)Image.FromStream(mm);

bmp.Save("test.bmp");

mm.Close();

return bmp;

}
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 16:39
non... il faut que tu procèdes comme ton code d'exemple... mais avec un memory stream (cette classe contient aussi une méthode Write)

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
13 juin 2005 à 17:09
Salut, J'ai pas tout lu, c'est trop long... mais juste une question : Vous etes sur que la fonction Image.FromStream elle attend des donnees aux format GDI ? Elle attend peut etre juste le stream d'une image GDI+ !? Vu que j'ai pas tout lu je suis peut etre a cote de la plaque.. : )
0
astragoth66 Messages postés 31 Date d'inscription lundi 6 juin 2005 Statut Membre Dernière intervention 2 octobre 2005
13 juin 2005 à 17:40
Quelle est la différence entre une image GDI et une image GDI+ ???



voila ce que dit la msdn de fromstream:

Creates an [frlrfsystemdrawingimageclasstopic.htm Image] object
from the specified data stream.



[Visual Basic] [frlrfsystemdrawingimageclassfromstreamtopic1.htm Overloads Public Shared
Function FromStream(Stream) As Image]

[C#] [frlrfsystemdrawingimageclassfromstreamtopic1.htm public static Image
FromStream(Stream);]

[C++] [frlrfsystemdrawingimageclassfromstreamtopic1.htm public: static Image*
FromStream(Stream*);]

[JScript] [frlrfsystemdrawingimageclassfromstreamtopic1.htm public static function
FromStream(Stream) : Image;]
voila
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
13 juin 2005 à 18:27
Une image GDI+ c'est la classe Image de System.Drawing.. Peut etre que la fonction FromStream attend juste une classe System.Drawing.Image ou System.Drawing.Bitmap sous forme de stream.. ?? Meme si en interne elle utilise surement les fonctions GDI32.
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
13 juin 2005 à 18:32
Sinon, un ptit detail.. tu devrais utiliser le code unsafe et les pointeurs, parce que "Marshal.ReadByte( new IntPtr( address ) );" pour du traitement video ca va pas le faire au niveau des performances !
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
13 juin 2005 à 18:54
Tiens un ctor qui peut correspondre a ce que tu veux faire.. ici.
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
13 juin 2005 à 19:33
la méthode FromStream de la classe Bitmap accepte tout flux correctement construit et représentant une image (ex: .GIF, .PNG, .JPG...) donc, aucun rapport entre GDI et GDI+.

ton problème vient du fait que tu ne construis pas proprement ton flux dans ton memorystream.

Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
</FO< body>
0
Rejoignez-nous