merge two picturebox and save - c#

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);
}
}
}

Related

Code for filtering bitmap images or QR code

This is my code, for decoding a bitmap. I want to make it so when the user chooses an image or bitmap that doesn't contain a QR code, it will say something like "Image does not contain code", or when the image is too small.
This error shows up when I try and decrypt a bitmap that doesn't have a QR code.
private void btnDecode_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false})
{
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(ofd.FileName);
MessagingToolkit.QRCode.Codec.QRCodeDecoder decoder = new MessagingToolkit.QRCode.Codec.QRCodeDecoder();
txtDecode.Text = decoder.Decode(new QRCodeBitmapImage(pictureBox1.Image as Bitmap));
}
}
}
And here is how my program encrypts text. It makes the text inputted in the textbox a bitmap, then saves it as JPEG.
private void button1_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true })
{
if (sfd.ShowDialog() == DialogResult.OK)
{
MessagingToolkit.QRCode.Codec.QRCodeEncoder encoder = new MessagingToolkit.QRCode.Codec.QRCodeEncoder();
encoder.QRCodeScale = 8;
Bitmap bmp = encoder.Encode(txtEncode.Text);
pictureBox1.Image = bmp;
bmp.Save(sfd.FileName, ImageFormat.Jpeg);
}
}
}
Please do note that I'm new to c#

Can't open video picturebox using Opencv library

I can't open video in picturebox My code is:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.FileName = "*.*";
if (open.ShowDialog() == DialogResult.OK)
{
isAnyText = false;
// string text = open.FileName;
// MessageBox.Show("resimin yolu :" + text);
/// CvVideoWriter video = new CvVideoWriter();
// videoSource.NewFrame += new NewFrameEventHan
CvCapture capture = new CvCapture(open.FileName);
pictureBox1.Image = new Bitmap(capture);
// pictureBox1.Image = capture;// new Bitmap(pictureBox1.Image);
//pictureBox1.Capture = new Bitmap(open.FileName);
// pictureBox1.Image.
string plaka = ft.GetImageText(open.FileName);
var img = pictureBox1.Image;
}
foreach (var s in ft.textList)
listBox1.Items.Add(s);
}
I have a problem here
pictureBox1.Image = new Bitmap(capture);
At the same time I try this:
pictureBox1.Capture = new Bitmap(open.FileName);
and I try lots of things but I cant do it
Any advance?

How to load a image into a picture box?

I Am programming in C# and I Already have it that I can select my files from my phone with this text:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
var FileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
FileOpenPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
FileOpenPicker.FileTypeFilter.Add(".jpg");
FileOpenPicker.FileTypeFilter.Add(".jpeg");
FileOpenPicker.FileTypeFilter.Add(".png");
FileOpenPicker.PickSingleFileAndContinue();
How can I let the picture popup in the picturebox?
Use a FileDialog and load a bitmap from the selected path:
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Create a new Bitmap object from the picture file on disk,
// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}

Confusion about OpenFileDialog

I am following this tutorial and downloaded source code to practice, and it works. The problem occurs when I rewrite the code: just one image is added instead of all the selected images. What am I doing wrong here?
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
"All files (*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach (string name in ofd.FileNames)
{
PictureBox imageControl = new PictureBox();
imageControl.Width = 100;
imageControl.Height = 100;
Image.GetThumbnailImageAbort CallBck = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(name);
Image img = myBitmap.GetThumbnailImage(97, 97, CallBck, IntPtr.Zero);
imageControl.Image = img;
panel1.Controls.Add(imageControl);
}
}
}
I'll bet they are all being added but they're just all going on top of each other at location (0,0) in the panel (you should step through your code to check this though).
The solution: Either manually specify a location for each new PictureBox, or use a layout control such as a FlowLayoutPanel.

Load a bitmap image into Windows Forms using open file dialog

I need to open the bitmap image in the window form using open file dialog (I will load it from drive). The image should fit in the picture box.
Here is the code I tried:
private void button1_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Title = "Open Image";
dialog.Filter = "bmp files (*.bmp)|*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
var PictureBox1 = new PictureBox();
PictureBox1.Image(dialog.FileName);
}
dialog.Dispose();
}
You have to create an instance of the Bitmap class, using the constructor overload that loads an image from a file on disk. As your code is written now, you're trying to use the PictureBox.Image property as if it were a method.
Change your code to look like this (also taking advantage of the using statement to ensure proper disposal, rather than manually calling the Dispose method):
private void button1_Click(object sender, EventArgs e)
{
// Wrap the creation of the OpenFileDialog instance in a using statement,
// rather than manually calling the Dispose method to ensure proper disposal
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
// Create a new Bitmap object from the picture file on disk,
// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
Of course, that's not going to display the image anywhere on your form because the picture box control that you've created hasn't been added to the form. You need to add the new picture box control that you've just created to the form's Controls collection using the Add method. Note the line added to the above code here:
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(dlg.FileName);
// Add the new control to its parent's controls collection
this.Controls.Add(PictureBox1);
}
}
}
Works Fine.
Try this,
private void addImageButton_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//For any other formats
of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
if (of.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = of.FileName;
}
}
You should try to:
Create the picturebox visually in form (it's easier)
Set Dock property of picturebox to Fill (if you want image to fill form)
Set SizeMode of picturebox to StretchImage
Finally:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox1.Image = Image.FromFile(dlg.Filename);
}
dlg.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
You, can also try like this, PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);
PictureBox.Image is a property, not a method. You can set it like this:
PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
You can try the following:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select file to be upload";
fDialog.Filter = "All Files|*.*";
// fDialog.Filter = "PDF Files|*.pdf";
if (fDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fDialog.FileName.ToString();
}
}
It's simple. Just add:
PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

Categories

Resources