Image scaling

BlitzMax Forums/BlitzMax Beginners Area/Image scaling

I'm trying to write a method that scales an image file. As SetScale gives a bit of a blurry and anti-aliased effect and the images I'm trying to scale have very few pixels I think it would be better to write a custom method. What I want is that every pixel in the image gets multiplied by a given amount; so when you're scaling an image by the factor 4, every pixel in the image will turn into a 4x4 rectangle.
I hope this does not sound to confusing. I think the function I have works, but that I'm doing something wrong with the reading/writing/creating of pixmaps/images. I don't have much experience with the modification of pixmaps, so could someone please have a glance at my method and look what is wrong?
	Method scaleImage(_im:TImage, _sx:Int, _sy:Int)
		Local im:TImage, pm:TPixmap, pm2:TPixmap, w:Int, h:Int
		Local x:Int, y:Int, x2:Int, y2:Int, pix:Int
		
		w = _im.width
		h = _im.height
		im = TImage.Create(w, h, 1, 0, 0, 0, 0)
		pm2 = CreatePixmap(w * _sx, h * _sy, PF_RGB888)
		
		DrawImage(_im, 0, 0)
		pm = GrabPixmap(0, 0, w, h);Cls
		
		For x = 0 Until w
			For y = 0 Until h
				For x2 = 0 Until _sx
					For y2 = 0 Until _sy
						WritePixel(pm2, pm.ReadPixel(x,y), x * _sx + x2, y * _sy + y2)
					Next
				Next
			Next
		Next
				
		im.pixmaps[0] = pm2
		DrawImage(im,0,0);Flip;WaitKey()
	End Method

I'm getting the error: "Unhandled exception: Pixmap coordinates out of bounds" (highlighting the WritePixel line).