c# - Converted Image to Gif, Works Partially? -


i've written short method converting images(image class) gifencoder , saving gif file. however, when go open resulting gif created, has problems. 1) gif stops playing after 2 cycles in browser. 2) when loaded through image editor colors seem mix bit between pixels.

public void converttogif( string destinationpath , image myimage , int myframes ) {     bitmap mybitmap = new bitmap( myimage.width / myframes , myimage.height );     gifbitmapencoder myencoder = new gifbitmapencoder();      int = 0;     while( < myframes ) {         graphics grdrw = graphics.fromimage( mybitmap );         var destregion = new rectangle( 0 , 0 , mybitmap.width , mybitmap.height );         var srceregion = new rectangle( mybitmap.width * , 0 , mybitmap.width , mybitmap.height );         grdrw.drawimage( myimage , destregion , srceregion , graphicsunit.pixel );         bitmapsource mysource = system.windows.interop.imaging.createbitmapsourcefromhbitmap( mybitmap.gethbitmap() , intptr.zero , int32rect.empty , bitmapsizeoptions.fromemptyoptions() );         myencoder.frames.add( bitmapframe.create( mysource , mysource ) );         grdrw.dispose();         += 1;     }      system.io.filestream mystream = new system.io.filestream( @destinationpath , system.io.filemode.create );     myencoder.save( mystream ); } 

the number of frames image created converttogif() don't seem register when running resulting gif through other method, convertfromgif():

    public image convertfromgif( image myimage ) {         var mydimensions = new framedimension( myimage.framedimensionslist[ 0 ] );         var myframes = myimage.getframecount( mydimensions );         var newimage = new bitmap( myimage.width * myframes , myimage.height );          for( int = 0; < myframes; ++ ) {             myimage.selectactiveframe( mydimensions , );             var destregion = new rectangle( myimage.width * , 0 , myimage.width , myimage.height );             var srceregion = new rectangle( 0 , 0 , myimage.width , myimage.height );              graphics grdrw = graphics.fromimage( newimage );             grdrw.drawimage( myimage , destregion , srceregion , graphicsunit.pixel );             grdrw.dispose();         }          return newimage;     } 

i can run gif through convertfromgif() method, gifs made converttogif() method won't work.

i had around , discovered gifbitmapencoder meant read gifs can displayed in wpf class allow create multi-framed gifs not provide metadata handler allows put key frame information cycle count, delay between frame etc. have left metadata part out 3rd parties implement

unfortunately don't expose metadata reader or writer gifs (animated or otherwise). in order produce correct animated gifs, need write out metadata timing information. since don't provide gif metadata handler, provide mechanism 3rd parties write own. documentation writing metadata handler can found here: http://msdn2.microsoft.com/en-us/library/ms737407.aspx in article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wiccodec.asp.

quote from: http://social.msdn.microsoft.com/forums/en-us/6ef358a7-d1ac-4267-91d9-166024aad8ca/creating-animated-gif-files-in-wpf?forum=wpf

gifs have default timing information getting 2 cycles may differ other browsers etc.

also while looking online saw other users having same issue colour bleed between frames.

there 2 avenues you: use 3rd party .net library allows write gifs or write own.

from previous question convert list of images gif suggest use ngif - http://www.codeproject.com/articles/11505/ngif-animated-gif-encoder-for-net

however if want create own metadata writer gifbitmapencoder follow links provided on post above documentation metadata read , writers.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -