• 0

Draw image every 50 pixels.. Help? vb.net 2010 Q


Question

Hello Community, (New Guy Here qith vb.net 2010 question)
 
Im struggling to figure this out.
 
What im trying to do is draw either a picturebox or image then skip 50 pixels then draw another image or picbox and repeat this process. I need this to happen across the entire window form and stop the the end width pixel of the form.
 
Possible? I figure it might be some sort of loop or array maybe even both.. but im new to this area of VB.net programming so im hoping for help.

If it means anything what i am trying to do here is something ive been trying to accomplish for months using vb6 and now vb.net and no luck.

Thanks in advance! You have my gratitude eternally you really have no idea seriously!

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I imagine you would just draw directly on the form/window. Override the form's OnPaint() event, get a reference to the Graphics class, then call the DrawImage function repeatedly. For each row, draw the image until x reaches the windows width. You're basically just tiling the image.

 

using System;
using System.Windows.Forms;
using System.Drawing;

class DrawingApp {

	public static void Main ( String[] args ) {
		Application.Run ( new MyForm () );
	}
}

class MyForm : Form {

	private const int GAP = 50;
	private Image tile;
	
	public MyForm () {
		tile = Image.FromFile ( "tile.png" );
	}

	protected override void OnPaint ( PaintEventArgs e ) {

		Point dest = new Point ( 0, 0 );

		for ( dest.Y = 0; dest.Y < Size.Height; dest.Y += tile.Height + GAP ) {

			for ( dest.X = 0; dest.X < Size.Width; dest.X += tile.Width + GAP )
				e.Graphics.DrawImageUnscaled ( tile, dest );
		} 		
	}	

}
 

Something like this. This is obviously in C#, but the VB should be very similar logic wise.

Link to comment
Share on other sites

  • 0

yes this is what i mean but it has to stop not keep going.

 

example: http://img.informer.com/screenshots/17/17338_2.jpg

 

exactly like this... (christmas lights at top of desktop) i need to do one line of images exactly like this... i have everything else done (desktop placement and trasnparency ect) but need the images dynamic or they either get cut off if i increase my screen res or look off when i lower it and would have to make a ton of images to ajust for any res i can think of (res being screen resolution)

Link to comment
Share on other sites

  • 0

yes this is what i mean but it has to stop not keep going.

So you mean one row?

example: http://img.informer.com/screenshots/17/17338_2.jpg

 

exactly like this... (christmas lights at top of desktop) i need to do one line of images exactly like this... i have everything else done (desktop placement and trasnparency ect) but need the images dynamic or they either get cut off if i increase my screen res or look off when i lower it and would have to make a ton of images to ajust for any res i can think of (res being screen resolution)

So you don't want a partial image is what you're saying. Well that's easy to fix. Just end the loop if it can't render a complete image:

using System;
using System.Windows.Forms;
using System.Drawing;

class DrawingApp {

	public static void Main ( String[] args ) {
		Application.Run ( new MyForm () );
	}
}

class MyForm : Form {

	private const int GAP = 50;
	private Image tile;
	
	public MyForm () {
		tile = Image.FromFile ( "tile.png" );
	}

	protected override void OnPaint ( PaintEventArgs e ) {
		
		for ( Point dest = new Point ( 0, 0 ); dest.X + tile.Width < Size.Width; dest.X += tile.Width + GAP )
			e.Graphics.DrawImageUnscaled ( tile, dest );
	}	

}
Link to comment
Share on other sites

  • 0

So you mean one row?

So you don't want a partial image is what you're saying. Well that's easy to fix. Just end the loop if it can't render a complete image:

using System;
using System.Windows.Forms;
using System.Drawing;

class DrawingApp {

	public static void Main ( String[] args ) {
		Application.Run ( new MyForm () );
	}
}

class MyForm : Form {

	private const int GAP = 50;
	private Image tile;
	
	public MyForm () {
		tile = Image.FromFile ( "tile.png" );
	}

	protected override void OnPaint ( PaintEventArgs e ) {
		
		for ( Point dest = new Point ( 0, 0 ); dest.X + tile.Width < Size.Width; dest.X += tile.Width + GAP )
			e.Graphics.DrawImageUnscaled ( tile, dest );
	}	

}

 

 

Forgive me, Ive never worked with C# and dont know how to write this in VB.net as opposed to C .net

 

i tried the converters but its messed up, By chance would you know how to write this in vb for me to experement with?

Link to comment
Share on other sites

  • 0

Forgive me, Ive never worked with C# and dont know how to write this in VB.net as opposed to C .net

 

i tried the converters but its messed up, By chance would you know how to write this in vb for me to experement with?

Imports System
Imports System.Windows.Forms
Imports System.Drawing

Module DrawingApp
	Sub Main ()
		Application.Run ( New MyForm () )
	End Sub
End Module

Class MyForm : Inherits Form

	Private Const GAP As Integer = 50
	Private tile As Image
	
	Public Sub New ()
		tile = Image.FromFile ( "tile.png" )
	End Sub

	Protected Overrides Sub OnPaint ( ByVal e As PaintEventArgs )
		
		Dim dest As Point = New Point ( 0, 0 )
		
		While dest.X + tile.Width < Size.Width 
			e.Graphics.DrawImageUnscaled ( tile, dest )
			dest.X = dest.X + tile.Width + GAP
		End While
		
	End Sub	

End Class
Link to comment
Share on other sites

This topic is now closed to further replies.