Can't open video picturebox using Opencv library - c#

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?

Related

C# and Ghostscript.net results in error (no picture)

I tried to create a little program to convert PDF to a TIF file using ghostscript but unfortunately it results in an error ("null"). Can't figure out why it's failing:
void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "PDF Files|*.pdf";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strfilename = openFileDialog1.FileName;
}
}
void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog targetfolder = new FolderBrowserDialog();
if (targetfolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folder = targetfolder.SelectedPath;
}
}
void button3_Click(object sender, EventArgs e)
{
const string DLL_64BITS = "gsdll64.dll";
string NomeGhostscriptDLL;
NomeGhostscriptDLL = DLL_64BITS;
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(NomeGhostscriptDLL);
///var xDpi = 300;
var yDpi = 300;
using (var rasterizer = new GhostscriptRasterizer())
{
byte[] buffer = File.ReadAllBytes(strfilename);
MemoryStream ms = new MemoryStream(buffer);
rasterizer.Open(ms, gvi, true);
int PdfPages = rasterizer.PageCount;
for (int pageNumber = 1; pageNumber < rasterizer.PageCount; pageNumber++)
{
string outputTIFPath = Path.Combine(folder, "00" + pageNumber.ToString() + ".tiff");
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
MessageBox.Show(outputTIFPath);
pdf2TIF.Save(outputTIFPath, ImageFormat.Tiff);
}
rasterizer.Close();
}
}
The error looks like this
Can anyone help me to sort this out?
try adding this
MyPlaceHolder.Controls.Add(pd2TIF);
below:
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
I just read that on a different thread. im not 100% sure if it works

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

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#

Display four images in four picture box in one time

I want to show four image in four picture box at a same time using (open file dialog and
menu strip) in c#, I used this code to print picture but not correct
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = null;
string str2 = null;
Bitmap img,img2;
int n=1;
OpenFileDialog opendialog1 = new OpenFileDialog();
opendialog1.InitialDirectory = "D:\\frames";
opendialog1.Filter = "Image File|*.bmp;";
opendialog1.Title = " Open Image file";
if (opendialog1.ShowDialog() == DialogResult.OK)
{
img = new Bitmap(opendialog1.FileName);
pictureBox1.Image = img;
str = opendialog1.FileName;
string name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\\frames"+name+".bmp");
pictureBox2.Image = img2;
str2 = opendialog1.FileName;
name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\\frames" + name + ".bmp");
pictureBox3.Image = img2;
str2 = opendialog1.FileName;
name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\\frames" + name + ".bmp");
pictureBox4.Image = img2;
str2 = opendialog1.FileName;
}
I need a method to appear four image at four picture box in one time
Your variables called name, str, str2, img2 and n are all superfluous to what you are trying to achieve.
Try this:
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap img;
OpenFileDialog opendialog1 = new OpenFileDialog();
opendialog1.InitialDirectory = "D:\\frames";
opendialog1.Filter = "Image File|*.bmp;";
opendialog1.Title = " Open Image file";
if (opendialog1.ShowDialog() == DialogResult.OK)
{
img = new Bitmap(opendialog1.FileName);
pictureBox1.Image = img;
img = new Bitmap("D:\\frames\\0001.bmp");
pictureBox2.Image = img;
img = new Bitmap("D:\\frames\\0002.bmp");
pictureBox3.Image = img;
img = new Bitmap("D:\\frames\\0003.bmp");
pictureBox4.Image = img;
}
}
You could even eliminate the img variable and assign the picture box images directly:
pictureBox1.Image = new Bitmap(opendialog1.FileName);
pictureBox2.Image = new Bitmap("D:\\frames\\0001.bmp");
pictureBox3.Image = new Bitmap("D:\\frames\\0002.bmp");
pictureBox4.Image = new Bitmap("D:\\frames\\0003.bmp");

c# A generic error occurred in GDI+

I have this problem when I drag to select region of the image in the picturebox more than two times and run the scanning. This is an OCR system.
region OCR(Tab4_Component)
//When user is selecting, RegionSelect = true
private bool RegionSelect = false;
private int x0, x1, y0, y1;
private Bitmap bmpImage;
private void loadImageBT_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = #"C:\Users\Shen\Desktop";
open.Filter = "Image Files(*.jpg; *.jpeg)|*.jpg; *.jpeg";
if (open.ShowDialog() == DialogResult.OK)
{
singleFileInfo = new FileInfo(open.FileName);
string dirName = System.IO.Path.GetDirectoryName(open.FileName);
loadTB.Text = open.FileName;
pictureBox1.Image = new Bitmap(open.FileName);
bmpImage = new Bitmap(pictureBox1.Image);
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
//User image selection Start Point
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RegionSelect = true;
//Save the start point.
x0 = e.X;
y0 = e.Y;
}
//User select image progress
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//Do nothing it we're not selecting an area.
if (!RegionSelect) return;
//Save the new point.
x1 = e.X;
y1 = e.Y;
//Make a Bitmap to display the selection rectangle.
Bitmap bm = new Bitmap(bmpImage);
//Draw the rectangle in the image.
using (Graphics g = Graphics.FromImage(bm))
{
g.DrawRectangle(Pens.Red, Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0));
}
//Temporary display the image.
pictureBox1.Image = bm;
}
//Image Selection End Point
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// Do nothing it we're not selecting an area.
if (!RegionSelect) return;
RegionSelect = false;
//Display the original image.
pictureBox1.Image = bmpImage;
// Copy the selected part of the image.
int wid = Math.Abs(x0 - x1);
int hgt = Math.Abs(y0 - y1);
if ((wid < 1) || (hgt < 1)) return;
Bitmap area = new Bitmap(wid, hgt);
using (Graphics g = Graphics.FromImage(area))
{
Rectangle source_rectangle = new Rectangle(Math.Min(x0, x1), Math.Min(y0, y1), wid, hgt);
Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt);
g.DrawImage(bmpImage, dest_rectangle, source_rectangle, GraphicsUnit.Pixel);
}
// Display the result.
pictureBox3.Image = area;
area.Save(#"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg");
singleFileInfo = new FileInfo("C:\\Users\\Shen\\Desktop\\LenzOCR\\TempFolder\\tempPic.jpg");
}
private void ScanBT_Click(object sender, EventArgs e)
{
var folder = #"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile";
DirectoryInfo directoryInfo;
FileInfo[] files;
directoryInfo = new DirectoryInfo(folder);
files = directoryInfo.GetFiles("*.jpg", SearchOption.AllDirectories);
var processImagesDelegate = new ProcessImagesDelegate(ProcessImages2);
processImagesDelegate.BeginInvoke(files, null, null);
//BackgroundWorker bw = new BackgroundWorker();
//bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
//bw.RunWorkerAsync(bw);
//bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void ProcessImages2(FileInfo[] files)
{
var comparableImages = new List<ComparableImage>();
var index = 0x0;
foreach (var file in files)
{
if (exit)
{
return;
}
var comparableImage = new ComparableImage(file);
comparableImages.Add(comparableImage);
index++;
}
index = 0;
similarityImagesSorted = new List<SimilarityImages>();
var fileImage = new ComparableImage(singleFileInfo);
for (var i = 0; i < comparableImages.Count; i++)
{
if (exit)
return;
var destination = comparableImages[i];
var similarity = fileImage.CalculateSimilarity(destination);
var sim = new SimilarityImages(fileImage, destination, similarity);
similarityImagesSorted.Add(sim);
index++;
}
similarityImagesSorted.Sort();
similarityImagesSorted.Reverse();
similarityImages = new BindingList<SimilarityImages>(similarityImagesSorted);
var buttons =
new List<Button>
{
ScanBT
};
if (similarityImages[0].Similarity > 70)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True";
con.Open();
String getFile = "SELECT ImageName, Character FROM CharacterImage WHERE ImageName='" + similarityImages[0].Destination.ToString() + "'";
SqlCommand cmd2 = new SqlCommand(getFile, con);
SqlDataReader rd2 = cmd2.ExecuteReader();
while (rd2.Read())
{
for (int i = 0; i < 1; i++)
{
string getText = rd2["Character"].ToString();
Action showText = () => ocrTB.AppendText(getText);
ocrTB.Invoke(showText);
}
}
con.Close();
}
else
{
MessageBox.Show("No character found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
i understand the reason it occur is that the image has been duplicated. But I have no idea how to solve it.
First off this looks like a dublicate of this topic:
c# Bitmap.Save A generic error occurred in GDI+ windows application
You also authored that question. From what I can tell it's the same question regarding the same code.
You mention this happens the second time you select an area, and both time you save the image to the same path. You also say the errors occurs when saving.
I think that would be a very strong indication of a permission error. Have you tried saving to a new file name every time as a test?
If it's a permission error then you simply need to dispose of any resources that have taken a lock on that file.
There are plenty of examples of this out there:
http://www.kerrywong.com/2007/11/15/understanding-a-generic-error-occurred-in-gdi-error/
public void Method1()
{
Image img = Image.FromFile(fileName);
Bitmap bmp = img as Bitmap;
Graphics g = Graphics.FromImage(bmp);
Bitmap bmpNew = new Bitmap(bmp);
g.DrawImage(bmpNew, new Point(0, 0));
g.Dispose();
bmp.Dispose();
img.Dispose();
//code to manipulate bmpNew goes here.
bmpNew.Save(fileName);
}
There could however be other issues. If you get the image from a stream, this stream needs to remain open until you're done with the image. (When you dispose of the image you'll automatically dispose of the stream.)
I can't see anything like that in the code you've posted though.
If you are using a 3rd party library for the OCR part, it could also have taken a lock on the resource.
A good place to read about this would be here:
http://support.microsoft.com/?id=814675
However from all you've said it simply sounds like there's a lock on the file you try to save to. So as mentioned above I would start off by simply trying to give the file a new name each time. If it doesn't work then you can start exploring other possibilities.
Quick and dirty example:
area.Save(#"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic-" + Guid.NewGuid().ToString() + #".jpg");
You should try this before dismissing a permission issue.

Categories

Resources