How can I access picturebox1 from my ocr.Image = img bit of the code. I have it working if I load it by using
ocr.Image = ImageStream.FromFile("C:/Users/John/Pictures/3.jpg"); // Give the image to the library
but obviously thats not what I am tryng to achive.
Error is:
cannot implicitly convert type system.drawing.image to
apose.ocr.iimagestream
private void timer1_Tick(object sender, EventArgs e)
{
//SendKeys.Send("{PRTSC}");
Image img = Clipboard.GetImage();
pictureBox1.Image = img;
//ocr processing
ocr.Image = img; // Give the image to the library
if (ocr.Process()) // Start processing it
{
label1.Text = "Text: " + ocr.Text;
}
}
Try this:
if(img!=null)
{
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg); // put here the image format
ms.Position = 0;
ocr.Image = ImageStream.FromStream(ms,ImageStreamFormat.Jpg);
..
..//all your processing stuff
}
Related
I have the following code. I want to use pictureBox1.Image = image outside of the code block like so:
private void button1_Click(object sender, EventArgs e) {
using(MemoryStream memoryStream = new MemoryStream()) {
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
}
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
}
I get the folloowing error: The name 'memoryStream' does not exist in the current context. I know I can do the following move the last 2 lines into the brackets and the code works but How can I use variable outside of the { } block of code
I know the code below works but i just want to know if there is a way to use pictureBox1.Image = image outside of the block of code.
private void button1_Click(object sender, EventArgs e)
{
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
}
}
You can't and shouldn't use the stream outside of the using scope, after that it will get disposed and you don't run into leaking issues.
You could just assign the value after the scope, when you declare the variable before it.
private void button1_Click(object sender, EventArgs e)
{
Image image;
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
// byte[] imageBytes = memoryStream.ToArray(); //unused?
image = Image.FromStream(memoryStream);
}
pictureBox1.Image = image;
}
Or better yet make a function to read the image and return it.
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = readImage();
}
private Image readImage()
{
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
// byte[] imageBytes = memoryStream.ToArray(); //unused?
return Image.FromStream(memoryStream);
}
}
There's no problem putting that line outside the using block. The problem is line before. You can't use memoryStream outside the using block. The whole point of the using block is to create a scope for that variable and ensure that the object assigned to it is disposed at the end of the block. You can do this:
private void button1_Click(object sender, EventArgs e) {
Image image;
using(MemoryStream memoryStream = new MemoryStream()) {
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
image = Image.FromStream(memoryStream);
}
pictureBox1.Image = image;
}
You can define MemoryStream outside the code block:
MemoryStream memoryStream = new MemoryStream();
using (memoryStream)
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
}
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
Edit:(just for test)
If you think using releases all resources for the variable defined outside, test the following code:
MemoryStream memoryStream = new MemoryStream();
using (memoryStream)
{
byte[] messageBytes = Encoding.ASCII.GetBytes("test Massage");
memoryStream.Write(messageBytes, 0, messageBytes.Length);
}
var data = Encoding.Default.GetString(memoryStream.ToArray()); // data = "test Massage"
I am trying to enlarge the size of screenshot without losing quality (as possible), but I can not do this. I am processing this picture in another method and filestream stop working. Actually tesseract can not read because of the screenshot's size so I am trying to enlarge the size of screenshot but I can not change the size of screenshot during capturing.
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(4000);
Snapshot().Save("D:\\program_goruntusu.jpg");
string s = FotoAnaliz();
}
private Bitmap Snapshot()
{
Bitmap Screenshot = new Bitmap(20, 20);
Graphics GFX = Graphics.FromImage(Screenshot);
GFX.CopyFromScreen(1243, 349, 0, 0, new Size(20, 20));
return Screenshot;
}
private string FotoAnaliz()
{
FileStream fs = new FileStream("D:\\program_goruntusu.jpg", FileMode.OpenOrCreate);
//string fotopath = #"D:\\program_goruntusu.jpg";
Bitmap images = new Bitmap(fs);
using (var engine = new TesseractEngine(#"./tessdata", "eng"))
{
engine.SetVariable("tessedit_char_whitelist", "0123456789");
// have to load Pix via a bitmap since Pix doesn't support loading a stream.
using (var image = new Bitmap(images))
{
using (var pix = PixConverter.ToPix(image))
{
using (var page = engine.Process(pix))
{
sayı = page.GetText();
MessageBox.Show(sayı);
fs.Close();
}
}
}
}
return sayı;
}
I am working on an asp.net C# webforms project. I have to debug an issue where once in a while I get "A generic error occurred in GDI+" error. This may be due to too many people accessing the site at the same time. In order to mimic it, I want to create a for loop to create thousands of images and display it through the placeholder. The problem is that, the loop runs and only the last image shows up. I would like to see each and every image created in the loop to be displayed on the page. The following is the code:
protected void btnGenerateBarCode_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
var codabar = new ZXing.BarcodeWriter();
codabar.Options = options;
codabar.Format = ZXing.BarcodeFormat.CODE_128;
for (int i = 1000; i < 2000; i++)
{
using (Bitmap bitMap = new Bitmap(codabar.Write(i.ToString())))
{
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(byteImage);
}
//the placeholder control on the page
plBarCode.Controls.Add(imgBarCode);
}
}
}
The problem is that, the loop runs and only the last image shows up.
This is because you actually create only one control outside of your loop. Add the image creation inside the loop as:
protected void btnGenerateBarCode_Click(object sender, System.EventArgs e)
{
var codabar = new ZXing.BarcodeWriter();
codabar.Options = options;
codabar.Format = ZXing.BarcodeFormat.CODE_128;
for (int i = 1000; i < 2000; i++)
{
using (Bitmap bitMap = new Bitmap(codabar.Write(i.ToString())))
{
// Here create the image control
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(byteImage);
}
//the placeholder control on the page
plBarCode.Controls.Add(imgBarCode);
}
}
}
My code currently looks like this:
if (fe == "CR2")
{
Image img = null;
byte[] ba = File.ReadAllBytes(open.FileName);
using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
img = raw;
}
Bitmap bm = new Bitmap(img);
pictureBox1.Image = bm;
statusl.Text = fe;
}
When I open a RAW image the program stops and Visual Studio says:
Parameter is not valid: Image raw = Image.FromStream(new MemoryStream(ba))
Please help! How can I get a RAW file to show in a PictureBox ?
Create the bitmap like this:
Bitmap bmp = (Bitmap) Image.FromFile(open.FileName);
or without using bitmap:
this.pictureBox1.Image = Image.FromFile(open.FileName);
Example WPF:
BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(origFile),
BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
BitmapEncoder bmpEnc = new BmpBitmapEncoder();
bmpEnc.Frames.Add(bmpDec.Frames[0]);
Stream ms = new MemoryStream();
bmpEnc.Save(ms);
Image srcImage = Bitmap.FromStream(ms);
You're actually disposing an Image by specifying using (Image raw = Image.FromStream(new MemoryStream(ba))) later assigning the Disposed instance of image to picturebox which leads to this exception. To make to work you've to either don't dispose or clone the image.
Bitmap raw = Image.FromStream(new MemoryStream(ba) as Bitmap;
pictureBox1.Image = raw;
Or simply Clone
using (Image raw = Image.FromStream(new MemoryStream(ba)))
{
img = raw.Clone() as Bitmap;
}
Both of the above should work
you try this code :
private static void SaveImageToRawFile(string strDeviceName, Byte[] Image, int nImageSize)
{
string strFileName = strDeviceName;
strFileName += ".raw";
FileStream vFileStream = new FileStream(strFileName, FileMode.Create);
BinaryWriter vBinaryWriter = new BinaryWriter(vFileStream);
for (int vIndex = 0; vIndex < nImageSize; vIndex++)
{
vBinaryWriter.Write((byte)Image[vIndex]);
}
vBinaryWriter.Close();
vFileStream.Close();
}
private static void LoadRawFile(string strDeviceName, out Byte[] Buffer)
{
FileStream vFileStream = new FileStream(strDeviceName, FileMode.Open);
BinaryReader vBinaryReader = new BinaryReader(vFileStream);
Buffer = new Byte[vFileStream.Length];
Buffer = vBinaryReader.ReadBytes(Convert.ToInt32(vFileStream.Length));
vBinaryReader.Close();
vFileStream.Close();
}
How can I build video from stream image (only image without sound) in C#?
Here is some code of my application:
static int ii = 1;
public void drawBitmap(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
try
{
Bitmap b = new Bitmap(ms);
b.Save(#"c:\test\" + (ii++) + ".jpg");
Image i = (Image)b;
pictureBox1.Image = i;
ii++;
}
catch (Exception e)
{
}
}
I used the wrapper mentioned above, like this:
private static void hejHopp()
{
//http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library
//myReadyNAS device, got files via FTP from my webcam
var jpgFileList = Directory.EnumerateFiles(sourcePath,"*.jpg");
//load the first image
Bitmap bitmap = (Bitmap)Image.FromFile(jpgFileList.First());
//create a new AVI file
AviManager aviManager = new AviManager(sourcePath + "\\tada.avi", false);
//add a new video stream and one frame to the new file
//set IsCompressed = false
VideoStream aviStream = aviManager.AddVideoStream(false, 2, bitmap);
jpgFileList.Skip(1).ToList().ForEach(file =>
{
bitmap = (Bitmap)Bitmap.FromFile(file);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
});
aviManager.Close();
}
You might want to take a look at this thread:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f23caf50-85a9-4074-8328-176c8bcc393e/
Same question. Some answers.
More is also available here:
http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library