Monday, May 24, 2010

C# I need still help! 10 points for the best answer. C# C# C#?

Hello everybody.


I need a C# code for print an image, imported with a openFileDialog Component.


Can you help me?


Thanks

C# I need still help! 10 points for the best answer. C# C# C#?
First, you will need an OpenFileDialog, a PictureBox, and a method of starting the loading process (your choice, most likely a button's Click event).





Assuming the dialog is called openFileDialog and the picture box is called pictureBox, you can call this function (or copy and paste the code inside of it into the event handler.





void LoadImageFromDialog()


{


openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";


if (openFileDialog.ShowDialog() == DialogResult.OK)


{


pictureBox.Load(openFileDialog.FileNam...


}


}





Example Form:


Copy and paste this into a new file called ImageForm.cs and just create an instance of it and show it. Clicking on the form will open the file dialog and load the picture after selecting one.





using System;


using System.Windows.Forms;





public class ImageForm : Form


{


OpenFileDialog openFileDialog = new OpenFileDialog();


PictureBox pictureBox = new PictureBox();





public ImageForm()


{





this.pictureBox.Location = new System.Drawing.Point(0, 0);


this.pictureBox.Size = new System.Drawing.Size(300, 300);


this.pictureBox.Dock = DockStyle.Fill;


this.pictureBox.Click += new EventHandler( pictureBox_Click);





this.ClientSize = new System.Drawing.Size(300, 300);


this.Controls.Add(pictureBox);


this.Text = "Image Form";


}





void pictureBox_Click(object sender, EventArgs e)


{


openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";


if (openFileDialog.ShowDialog() == DialogResult.OK)


{


pictureBox.Load( openFileDialog.FileName);


}


}


}
Reply:Look in MSDN for Graphics.DrawImage, or just look at the Graphics class. You


draw an image in really the same way as you print anything.
Reply:Since we're talking windows forms, I think you want to create a form and add an OpenFileDialog to it, and add a PictureBox Control to it.





Add a button call it Load... in the OnClick you'll want to open the Dialog. When the user clicks Ok after selecting a picture. Set the image property of the PictureBox to the file in the Open dialog control.





Instant Gratification.


No comments:

Post a Comment