Help writeImageToStream?


#1

I was hanging when I run the program:

Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat* img_fileForm(0);
OutputStream *img_out(0);
img_fileForm->writeImageToStream(*img,*img_out);

#2

why not initializing the output steam first ?

  • on a NULL pointer is usually a bad idea.
    same thing for PNGImageFormat

#3

I was hanging when I run the program:

Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat* img_fileForm;
OutputStream *img_out;
img_fileForm->writeImageToStream(*img,*img_out);

#4
Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat* img_fileForm;
OutputStream *img_out;
img_fileForm->writeImageToStream(*img,*img_out);

In this case pointers would have garbage values since they were not initialized. If you want to use pointers of PNGImageFormat and OutputStream, you would have to new them.

Instead of using pointers, just create objects.

Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat img_fileForm;
OutputStream img_out;
img_fileForm.writeImageToStream(*img,img_out);

Hope it helps you.


#5

FWIW OutputStream is an abstract class so you will to use instead something like

Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat img_fileForm;
FileOutputStream img_out("outputFile.png");
img_fileForm.writeImageToStream(*img,img_out);
delete img;

Still this is a basic c++ question not related in anyway to Juce.
A good read:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html


#6

thanks otristan and vishvesh.

error:

FileOutputStream img_out("outputFile.png");

the right way

FileOutputStream img_out(File("imgsave.png"));

My final solution

Image* img=createComponentSnapshot(Rectangle(0,0,getWidth(),getHeight()));
PNGImageFormat img_fileForm;
MemoryOutputStream img_out;
if(img_fileForm.writeImageToStream(*img,img_out)==true){
      File f= File("imgsave.png");
      FileOutputStream* fsaveImg=f.createOutputStream();
      fsaveImg->setPosition(0);    
      fsaveImg->write(img_out.getData(),img_out.getDataSize());
      fsaveImg->flush();
      delete fsaveImg;
}
delete img;

thank you very much.