imageactionscript-3image-processingjpegfilereference

Error on saving images


I'm trying to save the content of a MovieClip in a JPEG image, but when I save it the image has this problem:

There is a blank space in it (obvious from quote formatting), this appears in all the images that I try to save using filereference with JPGEncoder.

I believe that the problem might be happening because of the JPGEncoder class but I'm not sure about it.

This is the function I'm using to save the image (some of the strings are in portuguese):

private function fl_Salvar(event:MouseEvent)
        {
            try
            {
                var src:BitmapData = new BitmapData(imageViewer.width,imageViewer.height);


                var mtx:Matrix = DisplayUtils.fitIntoRect(imageViewer.mcImage.getChildAt(0),rect,true,Alignment.MIDDLE,false);

                src.draw(imageViewer,mtx,null,null,null,true);

                var jpgEncoder:JPGEncoder = new JPGEncoder(85);
                var imgStream:ByteArray = null; 
                imgStream = jpgEncoder.encode(src);

                var file:FileReference = new FileReference();
                file.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
                file.save( imgStream, "TESTE.jpg");
            }
            catch (ioe:IllegalOperationError)
            {
                trace("Operação Ilegal.");
            }
            catch (ae:ArgumentError)
            {
                trace("Argumento Inválido.");
            }
            catch (me:MemoryError)
            {
                trace("Memória Insuficiente.");
            }
            catch (error:Error)
            {
                trace("Erro ao tentar salvar imagem : "
                              + " . Erro : " + error);
            }
        }

        private function ioErrorHandler( event:IOErrorEvent ):void
        {
            trace("Handler de erro I/O: " + event);
        }

I would like to know if someone knows what might be causing this. Thanks in advance.

Edit:

Here is my rect declaration:

rect = new Rectangle(imageViewer.mcImage.x,imageViewer.mcImage.y,imageViewer.mcImage.width,imageViewer.mcImage.height);

Solution

  • It's because of these two lines:

    var mtx:Matrix = DisplayUtils.fitIntoRect(imageViewer.mcImage.getChildAt(0),rect,true,Alignment.MIDDLE,false);
    src.draw(imageViewer,mtx,null,null,null,true);
    

    What they do is that they fit your image inside some rect. You haven't provided info what's that rect and how it's defined, but I guess it has some dimensions. So you are fitting image in, and it's not stretched, but resized on the longer side.

    Encoder works well, you just need to check the rectangle that you want to fit the image in.