Need help calling a method - c#

I am trying to upload a picture to a winform and then show a thumbnail. I tried adding the functionality to my btnUpload_click method but it would not allow me to set PaintEventArgs as an eventhandler. So to remedy this, I created another method but now need to know how to call it.
private void btnUpload_Click(object sender, EventArgs e)
{}
public void getImage(PaintEventArgs ex)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
upload = new Bitmap(open.FileName);
pictureBox1.Image.GetThumbnailImage(114, 108, myCallback, IntPtr.Zero);
ex.Graphics.DrawImage(upload, 150, 75);
}
}
Thank you for your assistance

You don't need PaintEventArgs for a Graphics instance. Just change the code to work inside the button click:
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
upload = new Bitmap(open.FileName);
pictureBox1.Image.GetThumbnailImage(114, 108, myCallback, IntPtr.Zero);
this.CreateGraphics().DrawImage(upload, 150, 75);
}

Related

merge two picturebox and save

I am developing a c# windows form application.. I want to merge two picturebox and save it to custom file location.. I used following code to do it. But it only save 2nd picturebox image. Could anybody tell me how to merge this two picturebox and save it..
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif;*.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp;*.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox2.Image = new Bitmap(open.FileName);
//combine two images
// image file path
textBox1.Text = open.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "JPG(*.JPG)|*.jpg";
if (dialog.ShowDialog() == DialogResult.OK)
{
previewimg.Image.Save(dialog.FileName);
pictureBox2.Image.Save(dialog.FileName);
}
}
}
Maybe need some adjustements (not tested) but I think it's useful:
using (var src1 = new Bitmap(#"c:\...\image1.png"))
using (var src2 = new Bitmap(#"c:\...\image2.png"))
using (var bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb))
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
g.DrawImage(src2, new Rectangle(src1.Width, 0, src2.Width, src2.Height));
bmp.Save(#"c:\...\combined.png", ImageFormat.Png);
}
UPDATE
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "JPG(*.JPG)|*.jpg";
if (dialog.ShowDialog() == DialogResult.OK)
{
var src1 = previewimg.Image;
var src2 = pictureBox2.Image;
using (var bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb))
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
g.DrawImage(src2, new Rectangle(src1.Width, 0, src2.Width, src2.Height));
bmp.Save(#"c:\...\combined.png", ImageFormat.Png);
}
}
}

how to save a bitmap and upload it in editable mode?

im doing a painter project in c# in windows forms application and im trying to save the drawing and upload it the picturebox . the image is load, but im not able to keep working on it . is there a way to save and load the a bitmap in 'mdl' format or something else so i could keep working on it ?
this is how i did it :
private void btn_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();// + "..\\myModels";
saveFileDialog1.Filter = "Images (.bmp;.jpg;.png)|.bmp;.jpg;.png";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
}
private void btn_Load_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(.jpg; *.jpeg; *.gif; *.bmp)|.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pic.Image = new Bitmap(open.FileName);
pic.Refresh();
}
}

Making a picturebox have multiple images like a gallery with previous/next buttons [c#]

My code so far works, except that the previous and next button only works for 2 images. Why? I don't get it,the list should be holding everything then why?
If I add 10 images, I wanna press previous and next all the way through the images.
int PageNumber = 0;
// A list of image filenames to display. You could populate this by
reading filenames from disk
List<string> ImageFilenames = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg;
*.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pctBox.SizeMode = PictureBoxSizeMode.CenterImage;
// display image in picture box
ImageFilenames.Add(open.FileName);
pctBox.Image = new Bitmap(open.FileName);
// image file path
//textBox1.Text = open.FileName;
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
//if there is no more pages
if (PageNumber == ImageFilenames.Count - 1)
{
// Move to the next page
--PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
private void btnNext_Click(object sender, EventArgs e)
{
// If we're not yet on the last page...
if (PageNumber < ImageFilenames.Count - 1)
{
// Move to the next page
++PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
Changed your logic a bit. Please find the code for reference
int nTotalNumber = 0;
int nCurrentItem = 0;
List<string> ImageFilenames = new List<string>();
private void LoadImage()
{
using (OpenFileDialog open = new OpenFileDialog())
{
open.Multiselect = true;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;*.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
string sFileName = open.FileName;
ImageFilenames = open.FileNames.ToList();
}
pbBox.Image = Image.FromFile(ImageFilenames[0]);
}
if (ImageFilenames.Count > 0)
nTotalNumber = ImageFilenames.Count;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
nCurrentItem--;
if (nCurrentItem < 0)
nCurrentItem = 0;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}
private void btnNext_Click(object sender, EventArgs e)
{
nCurrentItem++;
if (nCurrentItem > nTotalNumber)
nCurrentItem = nTotalNumber;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}

C# - Changing SizeMode of picturebox

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";
if(open.ShowDialog() == DialogResult.OK)
{
tbFileName.Text = open.FileName;
pictureBox1.Image = new Bitmap(open.FileName);
}
}
So I want to make an if statement, if the image is too big for the initial size of the image box (520, 301), set the image box sizemode to autosize, otherwise just put it in there.
I'm pretty sure that you can change it by using this:
picturebox1.SizeMode = PictureBoxSizeMode.AutoSize;
But I don't know how to write the if statement.
Just load your file to Bitmap and then compare its Height and Width property with our custom size (500 x 301). like
...
tbFileName.Text = open.FileName;
using (Bitmap bmp = new Bitmap(open.FileName))
{
if (bmp.Height >= 301 && bmp.Width >= 500)
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = bmp;
}
You could store your image temporary before assigning it to your picturebox and compare the size of it with the size of your box.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap tmp = new Bitmap(open.FileName);
if(tmp.Height >= pictureBox1.Height || tmp.Width >= pictureBox1.Width)
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = tmp;
}
}

Check the width and height of an image

I am able to display the picture in the picture box without checking the file size by the following code:
private void button3_Click_1(object sender, EventArgs e)
{
try
{
//Getting The Image From The System
OpenFileDialog open = new OpenFileDialog();
open.Filter =
"Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap img = new Bitmap(open.FileName);
pictureBox2.Image = img;
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
I want to check the image size for example whether it is 2MB or 4MB before displaying in the picture box. i also want to check the width and height of the image.
The Bitmap will hold the height and width of the image.
Use the FileInfo Length property to get the file size.
FileInfo file = new FileInfo(open.FileName);
var sizeInBytes = file.Length;
Bitmap img = new Bitmap(open.FileName);
var imageHeight = img.Height;
var imageWidth = img.Width;
pictureBox2.Image = img;
try
{
//Getting The Image From The System
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
System.IO.FileInfo file = new System.IO.FileInfo(open.FileName);
Bitmap img = new Bitmap(open.FileName);
if (img.Width < MAX_WIDTH &&
img.Height < MAX_HEIGHT &&
file.Length < MAX_SIZE)
pictureBox2.Image = img;
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
UWP has currently a nice interface to obtain image properties.
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
ImageProperties IP = await file.Properties.GetImagePropertiesAsync();
double Width = IP.Width;
double Height = IP.Height;
}
I had a similar issue and I wrote a method to detect if the picture is landscape or not.
If it can help you.
public static bool IsPictureLandscape(string fileName)
{
try
{
Bitmap image = new Bitmap(fileName);
return image.Width > image.Height;
}
catch (Exception)
{
return false;
}
}

Categories

Resources