A Picturebox Problem

A PictureBox Problem

I'll add another example that according to the updated requirement allows for moving image3.

To get it working, put an image with transparency in Resources\transp.png
This uses the same image for all three images, but you can simply replace transparentImg for image1 and image2 to suitable images.

Once the demo is started the middle image can be dragged-dropped around the form.

public partial class Form1 : Form
{
private readonly Image transparentImg; // The transparent image
private bool isMoving = false; // true while dragging the image
private Point movingPicturePosition = new Point(80, 20); // the position of the moving image
private Point offset; // mouse position inside the moving image while dragging
public Form1()
{
InitializeComponent();

//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(231, 235);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.Controls.Add(this.pictureBox1);
transparentImg = Image.FromFile("..\\..\\Resources\\transp.png");
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.DrawImageUnscaled(transparentImg, new Point(20, 20)); // image1
g.DrawImageUnscaled(transparentImg, new Point(140, 20)); // image2
g.DrawImageUnscaled(transparentImg, movingPicturePosition); // image3
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
var r = new Rectangle(movingPicturePosition, transparentImg.Size);
if (r.Contains(e.Location))
{
isMoving = true;
offset = new Point(movingPicturePosition.X - e.X, movingPicturePosition.Y - e.Y);
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
movingPicturePosition = e.Location;
movingPicturePosition.Offset(offset);
pictureBox1.Invalidate();
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMoving = false;
}
}

A picturebox import issue C#

From VS menu bar you can select Project -> Project properties(on the bottom) and then go to Resources tab. From here, you can add the resource(s) from disk, by hand. Then you can access them from code like this:

PictureBox pb = new PictureBox();
pb.Image = Properties.Resources.NameOfResource;

Don't forget to set the rest of PictureBox properties like Size, Location and so on ...

PictureBox image does not shrink, but PictureBox itself expand to fit the image

Based on your edit ... set globalPicBox.AutoSize = false and it'll work.

If it doesn't please check the rest of your code for changes to globalPicBox's AutoSize, SizeMode and Dock parameters and hopefully you'll find your answer!

WinForms picturebox not transparent

It is quite easy all you have to do is make the canvas PictureBox of the same size and set its location also the same as your first picture box. Then set canvas PictureBox back colour to transparent. Now set your first PictureBox as the parent of canvas PictureBox.

You can write the below code on the form load event.

pictureBoxCanvas.Size = pictureBox1.Size;
pictureBoxCanvas.Location = pictureBox1.Location;
pictureBoxCanvas.BackColor = Color.Transparent;
pictureBoxCanvas.BringToFront();
pictureBoxCanvas.Parent = this.pictureBox1;

Dynamically created PictureBox rendering problem

The easiest thing to do to fix the problem is something you've already tried - using directly a PictureBox instead of a Field. Now, considering that you only use Field to wrap a PictureBox, you could inherit from PictureBox instead of just wrapping it.
Changing your classes to these will fix the issue as you've noticed:

public abstract class Field : PictureBox {

public Field() {
Image = Image.FromFile(@"Bomb01.jpg");
SizeMode = PictureBoxSizeMode.StretchImage;
BorderStyle = BorderStyle.FixedSingle;
Size = new Size(100, 100);
}

// ...
// some abstract methods, not currently important
}

public class MemoryField : Field {
public MemoryField(Form parent, int xPos, int yPos, int xSize, int ySize) {
ClientSize = new Size(xSize, ySize);
Location = new Point(xPos, yPos);
}

// ...
}

The real reason it was not working has to do with both sizing and positioning of each Field and their subcomponents. You should not set the Location of each _pictureBox relatively to its parent MemoryField, but rather change the Location of the MemoryField relatively to its parent Form.

You should also set the size of your MemoryField to the size of its child _pictureBox otherwise it won't size correctly to fit its content.

public class MemoryField : Field {
public MemoryField(Form parent, int xSize, int ySize) {
_pictureBox.ClientSize = new Size(xSize, ySize);
// I removed the setting of Location for the _pictureBox.
this.Size = _pictureBox.ClientSize; // size container to its wrapped PictureBox
this.Parent = parent; // not needed
}
// ...
}

and change your creation inner loop to

for (int x = 0; x < 10; x++) // 10 columns
{
Field field = new MemoryField(this,
fieldWidth,
fieldHeight);
field.Location = new Point(x * (fieldWidth + 3), 0 + 0 + y * (fieldHeight + 3)); // Set the Location here instead!
this.Controls.Add(field);
}

Custom bitmap object isn't displaying correctly in PictureBox

As user Jimi pointed out in a comment, grp.PixelOffsetMode = PixelOffsetMode.Half from this post solved the issue.



Related Topics



Leave a reply



Submit