Pic in Circle


New member
Jul 5, 2018
Programming Experience
My next step in this code for me is to allow the user to move the pic from within the bounds of the circle

I would like the code to drive a mouse click and hold to move it

Then I want to have zoom functionality

Here is what I have so far...

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

namespace Sphere_Map
public partial class Form1 : Form
public Graphics gfx;

int radius = 300;
int y = 350;
int x = 350;

public Form1()
gfx= this.CreateGraphics();

private void button1_Click(object sender, EventArgs e)
Bitmap tmp = new Bitmap(2 * radius, 2 * radius);
Graphics g = Graphics.FromImage(tmp);
g.TranslateTransform(tmp.Width / 2, tmp.Height / 2);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0 - radius, 0 - radius, 2 * radius, 2 * radius);
Region region = new Region(path);
g.SetClip(region, CombineMode.Replace);
Bitmap bmp = new Bitmap("C://C# Graphics/Earth.jpg");
g.DrawImage(bmp, new Rectangle(-radius, -radius, 2 * radius, 2 * radius), new Rectangle(x - radius, y - radius, 2 * radius, 2 * radius), GraphicsUnit.Pixel);
tmp.Save("c://C# Graphics/US Boundary Map2.jpg");
gfx.DrawImage(Bitmap.FromFile("c://C# Graphics/US Boundary Map2.jpg"),new Point(100,100));


private void btnExit_Click(object sender, EventArgs e)
if (System.Windows.Forms.Application.MessageLoop)
// WinForms app
// Console app

Thank you for the assistance
Firstly, please don't post your code as HTML. Notice that all indenting has been lost, making reading it more difficult. Post your code as plain text inside appropriate formatting tags, i.e.

[xcode=c#]your code here[/xcode]

That will take care of syntax highlighting and retain indenting.

Secondly, NEVER call CreateGraphics. With the code you have, try minimising your form and then restoring it and you'll see that your drawing has disappeared, because all drawing is erased on a repaint. The proper approach is to store all the data describing the drawing in fields and then using those fields in the Paint event handler of the form or appropriate control. That way, your drawing will be done every time the form repaints. If the drawing needs to change, you simply change the data and then force a repaint.

That last sentence relates directly to your request too. When the user drags the drawing, you simply force a repaint when the drag is done and, optionally, while it's in progress and the drawing will appear to move from the user's perspective. You might like to check this out for some important concepts that will help you get going.
Top Bottom