# Center Dynamically Loaded JPGs of dif sizes

**URL:** <https://forum.kirupa.com/t/center-dynamically-loaded-jpgs-of-dif-sizes/186368>\
**Category:** flash\
**Created:** [April 20, 2006, 8:12pm UTC](https://forum.kirupa.com/t/center-dynamically-loaded-jpgs-of-dif-sizes/186368 "2006-04-20T20:12:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![station8](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/station8/32/2345_2.png) [@station8](https://forum.kirupa.com/u/station8)\
**Post date:** [April 20, 2006, 8:12pm UTC](https://forum.kirupa.com/t/center-dynamically-loaded-jpgs-of-dif-sizes/186368/1 "2006-04-20T20:12:32Z")

</div>

Hi All, I’ve read a few of the other threads on this subject but still had no luck. I’m loading jpgs via an xml file and the pics are all diffrent sizes. I’m trying to figure out how to center the image once loaded, but am running into brick wall after brick wall. Can anyone please help. This is not my code - i’m not at a point where i can write something like this myself - hence the confusion.

thanks in advance!

here’s the code:

```auto
this.createEmptyMovieClip("container_mc", this.getNextHighestDepth());
//container_mc._x=(Stage.width - container_mc._width)/2;
//container_mc._y=(Stage.height - container_mc._height)/2;
	
//container_mc._x = 80;
//container_mc._y = 20;

content = container_mc;
images_xml = new XML();
images_xml.onLoad = startImageViewer;
images_xml.load("images.xml");
images_xml.ignoreWhite = true;
function startPreloading(content) {
	preloader_mc._visible = true;
	preloader_mc.bar._xscale = 0;
	interval = setInterval(checkProgress,10, content);
}

function checkProgress(content) {
	var contentSize = 0;
	var contentLoaded = 0;
	var loaded = 0;
	contentSize = content.getBytesTotal();
	contentLoaded = content.getBytesLoaded();
	loaded = Math.ceil(contentLoaded / contentSize * 100);
    preloader_mc.bar._xscale = loaded;
	updateAfterEvent();
	if (contentLoaded == contentSize && contentSize > 0) {
		clearInterval(interval);
		preloader_mc._visible = false;
		
	}
	
}

function updateImage(newImageNode) {
	startPreloading(content);
	url = newImageNode.attributes.jpegURL;
    description = newImageNode.firstChild.nodeValue;
	description.htmlText;
    container_mc.loadMovie(url);
	
	
}

function startImageViewer(success) {
	        rootNode = images_xml.firstChild;
			totalImages = rootNode.childNodes.length;
			firstImageNode = rootNode.firstChild;
			currentImageNode = firstImageNode;
			currentIndex = 1;
			updateImage(firstImageNode);
			
}

next_btn.onRelease = function() {
	nextImageNode = currentImageNode.nextSibling;
	if (nextImageNode == null) {
		break;
	} else {
		currentIndex++;
		updateImage(nextImageNode);
		currentImageNode = nextImageNode;
	}
};

prev_btn.onRelease = function() {
	previousImageNode = currentImageNode.previousSibling;
	if (previousImageNode == null) {
		break;
	} else {
		currentIndex--;
		currentImageNode = previousImageNode;
		updateImage(previousImageNode);
	}
};

```

---

<div class="post-metadata">

**Author:** ![station8](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/station8/32/2345_2.png) [@station8](https://forum.kirupa.com/u/station8)\
**Post date:** [April 22, 2006, 5:20pm UTC](https://forum.kirupa.com/t/center-dynamically-loaded-jpgs-of-dif-sizes/186368/2 "2006-04-22T17:20:23Z")

</div>

just in case anyone runs across this with the same question, i figured it out. the jpg has to fully load before it can be centered, so this code works better for laoding/preloading the pix - and i actually wrote most of it myself =)

```auto
function updateImage(newImageNode) {
	//startPreloading(content);
	this.createEmptyMovieClip("imageHolderMC", 1);
	url = newImageNode.attributes.jpegURL;
    description = newImageNode.firstChild.nodeValue;
	this.createEmptyMovieClip("imageHolderMC", this.getNextHighestDepth());
	var loaderMC:MovieClipLoader = new MovieClipLoader();
	loaderMC.loadClip(url, imageHolderMC);
	var oListener:Object = new Object();
	oListener.onLoadInit = function() {
		centerImage();
		
	}
	
	loaderMC.addListener(oListener);

	oListener.onLoadProgress = function(imgeMC:MovieClip, loadedBytes:Number, totalBytes:Number) {
  	pText.text = "LOADING " + Math.round(loadedBytes / totalBytes*100) + "%";
	};
}

function centerImage() {
	imageHolderMC._x = Stage.width/2 - imageHolderMC._width/2;
	imageHolderMC._y = Stage.height/2 - imageHolderMC._height/2 - 50;
	pText.text = "";
	
}

```
