How can i replace cursor with bitmap in winform - c#

I am develeoping a magnifier on Mouse move control application in c#.net . I need to replace the cursor with the magnifier control(magnifier control is a picturebox). So is there anyway to accomplish this .

The example code below shows how to set a Cursor on windows form. The same approach can be used to set a Cursor for a Control too.
public class Form_With_A_Cursor_Example {
public void Shows_A_Form_With_A_Cursor_Loaded_From_A_pictureBox() {
Form frm = new Form();
PictureBox pb = new PictureBox() { Image = Image.FromFile( #"C:\Users\xxx\Pictures\someImage.bmp" ) };
frm.Cursor = new Cursor( ( (Bitmap)pb.Image ).GetHicon() );
frm.ShowDialog();
}
}

First add bitmap to your project resources:
Project->projectnameProperties->Add exiting file (from menu next to "Add Resource") add your BMP
Bitmap b = new Bitmap(projectname.Properties.Resources.yourCursorName);
b.MakeTransparent(b.GetPixel(0,0));
Graphics g = Graphics.FromImage(b);
IntPtr ptr = b.GetHicon();
Cursor = new System.Windows.Forms.Cursor(ptr);
Where "projectname" is the name of your project.

Related

Clicking on programmatically added pictureBox controls

I have a Winforms application where I programmatically add a number of pictureBox controls and load each one with an image.
How can I link each pictureBox control with a video file so I can run that video by clicking on that pictureBox control?
This is how I add my pictureBox controls:
if (ofd.ShowDialog() == DialogResult.OK)
{
file_address = ofd.FileName;
}
var picture1 = new PictureBox
{
Name = "pictureBox",
Size = new Size(160, 200),
Location = new Point(x, y),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Image.FromFile(file_address),
};
this.Controls.Add(picture1);
I've managed to add click event handler:
picture1.Click += delegate
{
// Do something
}
You can use the Tag property to store the file_address.
this.picture1.tag = file_address;
when you want to play the video by the picture box file name, get the file name by
string file_name = (string)this.picture1.tag;

How to set button backgroundimage in c#?

I'm new in this field. I'm working in windows application, I create panel and adding the buttons inside the panel. i want to retrieve the image from database and set to the background image in button.
This is my code,
FileName = objDR["Photopath"].ToString();
byte[] data = Encoding.UTF8.GetBytes(FileName);
MemoryStream ms = new MemoryStream(data);
image = new System.Drawing.Bitmap(ms);
Buttons[i].BackgroundImage = image;
initially put BackgroundImage property ON
OR
You can Code :
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));

How to position a borderless form under buttons

I have created 4 buttons dynamically and placed them horizontally using c# win forms.Now i want show a custom tooltip(actually its a borderless form) under each of the 4 buttons on mouse hover event.But how do i position my tooltip form under the buttons??
I have tried the code below but it does not work the desired way.
tooltip.Location = new System.Drawing.Point(b.Left, b.Top);
Where 'tooltip' is tooltip form object & 'b' is the dynamic button.Please advise with some code snippet.
private void B_MouseHover(object sender, EventArgs e)
{
var b = sender as Button;
//MessageBox.Show(Cursor.Position.ToString());
if(b!= null)
{
if (tooltip == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = new System.Drawing.Point(b.Left, b.Bottom);
tooltip.data(b.Tag.ToString());
tooltip.Show();
}
}
The way you named it is a bit misleading. As I understand, what you call a tooltip is just a Form. You need to consider 2 things
(1) Form.StartPosition must be set to FormStartPosition.Manual
(2) Form.Location must be in screen coordinates. Note that the Button.Location you are trying to use is in button's parent client coordinates. Control.PointToScreen has to be used for conversion.
In your case, it should be something like this
tooltip.StartPosition = FormStartPosition.Manual;
var topLeft = b.PointToScreen(new Point(0, 0));
tooltip.Location = new Point(topLeft.X, topLeft.Y + b.Height);
When you show the tooltip you can control its location, check show method overloads: https://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show.aspx

Trying to add a image to a group box, however, it doesnt show up

Solved: Using design toolbox.
I'm using the following method to add a image
private void cobertura1vertente_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("C:/cobertura1vertente.png");
pb1.Location = new Point(3, 3);
pb1.Size = new Size(250, 250);
//doubt right below
groupBox81.Controls.Add(pb1);
}
I usually would add a image to a entire form, but now i want to add it to a specific groupbox, who lies inside a tab.
If the code is correct, my guess is that the location that i've set is relative to the whole form, and not to the groupBox, why would that happen?

Open a image in a new form c#

I have a image showing in
picShowPicture.Image = Image.FromFile(textbox1.Text)
At the moment the image is showing on the main screen what I want is when a user selects a image from the database, it then open up in a new window?
how is this done?
Create a new Form in Designer and pick a PictureBox in it. And create a special method for example
public void SetPicture(Image image)
which will set image to PictureBox.
On selecting picture call:
YourForm form = new YourForm();
form.SetPicture(Image.FromFile(textbox1.Text));
form.ShowDialog();
Or you can dynamically create new form:
Form form = new Form();
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = Image.FromFile(textbox1.Text);
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
form.Controls.Add(pictureBox);
form.ShowDialog();
I'm going to make the assumption that by "selects a image from the database" does not mean that you're (de)serializing BLOBs and just want the code to make the image display in a new window. I will further assume that you have a second form created in your project called "Form2" with an image viewer called "picImageViewerOnForm2".
var newImage = Image.FromFile(textbox1.Text);
var newForm = new Form2();
newForm.picImageViewerOnForm2.Image = newImage;
newForm.Show();
Add Form to application and put PictureBox on it, let's say that ID of PictureBox is "pictureBox1", then create public proterty on that form to access picutre box, like this :
public partial class ShowPictureForm : Form
{
public PictureBox ImagePictureBox { get { return this.pictureBox1; } }
public ShowPictureForm()
{
InitializeComponent();
}
}
then show that new form like this :
ShowPictureForm spf = new ShowPictureForm();
spf.ImagePictureBox.Image.FromFile(textbox1.Text)
spf.ShowDialog();
Add a new Windows Form to your project, named "ShowImageWindow".
Add a picturebox to the window, and the following code:
public Image ImageToShow { get; set; }
public ShowImageWindow()
{
InitializeComponent();
}
private void ShowImageWindow_Load(object sender, EventArgs e)
{
pictureBox1.Image = ImageToShow;
}
Then create and show the window as follows:
Image img = Image.FromFile(textBox1.Text);
ShowImageWindow frm = new ShowImageWindow();
frm.ImageToShow = img;
frm.ShowDialog();
frm.Dispose();
Create a new Form (new type derived from System.Windows.Forms.Form) that accept image path as constructor.
Say new form is ImageForm. Create PictureBox inside this form.
in the function ShowImageWindow (or similar in main form), call like following
ImageForm imageForm = new ImageForm(textbox1.Text)
imageForm.ShowDialog()
In the ctor of ImageForm, set the Image into picturebox control inside ImageForm
public ImageForm(String imagePath)
{
pictureBox1.Image = Image.FromFile(imagePath);
}
Create a new form with just picture box in it.
Then when forming that forms object pass image as parameter(of course you have to create a parameterised constructor of your form) and pass that image in a global image variable.
In Form_Load set that global image variable as your image controls image.

Categories

Resources