I want to show all image files from dragged in window

#1

Hi, All.

I want to show all image files from dragged in window.
But this code just show one image.

What’s wrong to me?

#include “testApp.h”
//--------------------------------------------------------------
void testApp::setup(){

img1.load("/Users/mun/Desktop/aa.png");

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){

ofSetColor(255);
img1.draw(0,0, 512, 512);
ofSleepMillis(100);

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
if( dragInfo.files.size() > 0 ){
for(int i = 0; i < dragInfo.files.size(); i++){
img1.load(dragInfo.files[i]);
img1.update();
update();
ofLog() << dragInfo.files[i] << endl;
}// for
}// if
}

Thanks.

#2

Hi,
sure, it is because you are loading all into the same ofImage object, so each time you call load what was previously loaded gets deleted.
You need a vector<ofImage>

Try the following code.
in your ofApp.h file declare

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void draw();
	
		void dragEvent(ofDragInfo dragInfo);
	
		vector<ofImage> imgs;
};

then in ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::draw(){
	ofBackground(60);
	int imgsPerRow = 3; // change this if you want to display a different number of images on each row.
	float w = ofGetWidth()/(float)imgsPerRow;
	for(int i = 0; i < imgs.size(); i++){
		int x = i%imgsPerRow;
		int y = floor(i/imgsPerRow);
		ofRectangle gridRect(x * w, y * w, w, w); // I'll use a square grid for simplicity
		
		ofRectangle imgRect (0,0, imgs[i].getWidth(), imgs[i].getHeight());
		
		imgRect.scaleTo(gridRect); // this will scale the imgRect into the gridRect so we can draw our image
		
		ofNoFill();
		ofSetColor(150);
		ofDrawRectangle(gridRect);//just drawing the gridRect you can remove this if you want to
	
		ofSetColor(255);
		imgs[i].draw(imgRect);
	
	}
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 
	
		for(int i = 0; i < dragInfo.files.size(); i++){
			string ext = ofToLower(ofFilePath::getFileExt(dragInfo.files[i]));
			if( ext == "jpg" || ext == "jpeg" || ext == "png" ) {// just put here any extension you want to use
				ofImage img;
				if(img.load(dragInfo.files[i])){ //we check that the image was loaded properly
					imgs.emplace_back(std::move(img));// using emplace_back and move avoids copying the image
				}
			}
		}
}

BTW, dont put that ofSleepMillis(100); in draw. it is useless and will make your app to run slower and less responsive.

cheers

1 Like
#3

Thanks,

I’ll try that…

#4

Oops…

Your code shows all images at the same time and parallel place.

But What I want, is showing all images same place and sequencial time. --;
So, I want to use strcture of update() + draw() …

How Can, again? Sorry ~

Best,
@bemoregt.

#5

I see.
you could just store the vector of files you get from the dragEvent and then when you want to change the image you just load it into an ofImage instance.
so it should be something like:

in your ofApp.h file declare

#pragma once

#include "ofMain.h"
class ofApp : public ofBaseApp{
	public:
		void draw();
		void dragEvent(ofDragInfo dragInfo);
		void loadImg();
		vector<string> imgPaths;
		ofImage img;
                int currentImgIndex = -1; 


};

then in ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::draw(){
if(the image has to change){
loadImg();
}
	ofBackground(60);
	ofSetColor(255);
	img.draw(0,0);
}
//--------------------------------------------------------------
void ofApp::loadImg(){
if(imgPaths.size() > 0){
(++currentImgIndex) %= imgPaths.size();
img.load(imgPaths[currentImgIndex]);
}
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 
	imgPaths = dragInfo.files;
	currentImgIndex = -1; 
loadImg();
}
1 Like
#6

@roymacdonald

Thanks a lot.
It works well ~
ofSleepm() methos good works in update(){}

Cheers,
@bemoregt.

#7

Good to know.