gdi+ - How to create a gif image with C# -
i want create gif file several frames. want use method microsoft support--image.saveadd
but don't know how set encoderparameters paramater make gif file.
i can't find documents refer. how create gif file image.saveadd
probably, late useful original poster, managed create proper gif using system.drawing. code below based on jschroedl's answer, sets frame delays , number of animation loops.
// gdi+ constants absent system.drawing. const int propertytagframedelay = 0x5100; const int propertytagloopcount = 0x5101; const short propertytagtypelong = 4; const short propertytagtypeshort = 3; const inr uintbytes = 4; //... var gifencoder = getencoder(imageformat.gif); // params of first frame. var encoderparams1 = new encoderparameters(1); encoderparams1.param[0] = new encoderparameter(encoder.saveflag, (long)encodervalue.multiframe); // params of other frames. var encoderparamsn = new encoderparameters(1); encoderparamsn.param[0] = new encoderparameter(encoder.saveflag, (long)encodervalue.framedimensiontime); // params finalizing call. var encoderparamsflush = new encoderparameters(1); encoderparamsflush.param[0] = new encoderparameter(encoder.saveflag, (long)encodervalue.flush); // propertyitem frame delay (apparently, no other way create fresh instance). var framedelay = (propertyitem)formatterservices.getuninitializedobject(typeof(propertyitem)); framedelay.id = propertytagframedelay; framedelay.type = propertytagtypelong; // length of value in bytes. framedelay.len = bitmaps.count * uintbytes; // value array of 4-byte entries: 1 per frame. // every entry frame delay in 1/100-s of second, in little endian. framedelay.value = new byte[bitmaps.count * uintbytes]; // e.g., here, we're setting delay of every frame 1 second. var framedelaybytes = bitconverter.getbytes((uint)100); (int j = 0; j < bitmaps.count; ++j) array.copy(framedelaybytes, 0, framedelay.value, j * uintbytes, uintbytes); // propertyitem number of animation loops. var looppropertyitem = (propertyitem)formatterservices.getuninitializedobject(typeof(propertyitem)); looppropertyitem.id = propertytagloopcount; looppropertyitem.type = propertytagtypeshort; looppropertyitem.len = 1; // 0 means animate forever. looppropertyitem.value = bitconverter.getbytes((ushort)0); using (var stream = new filestream("animation.gif", filemode.create)) { bool first = true; bitmap firstbitmap = null; // bitmaps collection of bitmap instances that'll become gif frames. foreach (var bitmap in bitmaps) { if (first) { firstbitmap = bitmap; firstbitmap.setpropertyitem(framedelay); firstbitmap.setpropertyitem(looppropertyitem); firstbitmap.save(stream, gifencoder, encoderparams1); first = false; } else { firstbitmap.saveadd(bitmap, encoderparamsn); } } firstbitmap.saveadd(encoderparamsflush); } // ... private imagecodecinfo getencoder(imageformat format) { imagecodecinfo[] codecs = imagecodecinfo.getimagedecoders(); foreach (imagecodecinfo codec in codecs) { if (codec.formatid == format.guid) { return codec; } } return null; }
Comments
Post a Comment