1

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);
Raphael Rosa
  • 45
  • 1
  • 5

2 Answers2

1

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.

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • actually I edited my answer with the rect declaration and it should have the correct size of the movieclip... this is what I'm not understanding the rect has the right size and yet the image does not. – Raphael Rosa Jun 02 '14 at 14:39
  • 1
    Just trace the sizes before and after the `fitIntoRect` function. – Andrey Popov Jun 02 '14 at 14:54
1

Are you sure it is not you picture viewing software that is doing this? The image seems complete (they are written out to a byteArray pixel by pixel from top left to lower right.) if it were the file stream or byte array that were the trouble I would expect to see a distorted picture.

Is there actually a part of the image missing, or is that the full image?

schwack
  • 88
  • 6
  • the image is full... the only problem is that blank area on the right... I'm trying everything to remove that but no success – Raphael Rosa Jun 03 '14 at 13:37
  • You are simply putting a little image into a big frame. Your rectangle is a predefined size, but I do not see the actual source of the image itself. You can draw any size image into a bitmapData object, those that are too big get clipped, those that are too small get rendered to the origin, leaving empty space all around. Look at the source of you imageViewer object. It most likely explicitly sets dimensions before any image is actually added. You need to size the viewer to the image, not simply put the image into a pre-defined box. – schwack Jun 03 '14 at 17:39