Hi I need to convert an unmanaged image to a managed bitmap image which I need to display on a picturebox but I it seems to throw an exception saying "object reference not set to an instance of an object". Does anyone have an idea about it? I have commented the line which throws the exception.
for (int i = 0; i < characters.Length; i++)
{
area = characters[i].Area;
UnmanagedImage numer = characters[i].Image;
System.Drawing.Image plateImage = numer.ToManagedImage();//Exception
numberplate = new Bitmap(new Bitmap(plateImage));
pictureBox2.Image = numberplate;
pictureBox2.Refresh();
}
I am using Aforge.net framework with C#
UPDATE
for (int i = 0; i < characters.Length; i++)
{
area = characters[i].Area;
Bitmap numer = characters[i].Image.ToManagedImage();
//System.Drawing.Image plateImage = numer.ToManagedImage();
//numberplate = new Bitmap(new Bitmap(plateImage));
pictureBox2.Image = numberplate;
pictureBox2.Refresh();
}
I found this code on the Aforge.Net forums and it seemed to work.
BlobCounterBase bc = new BlobCounter();
bc.FilterBlobs = true;
bc.MinHeight = 5;
bc.MinWidth = 5;
bc.ProcessImage(numberplate);
Blob[] blobs = bc.GetObjectsInformation();
MessageBox.Show(bc.ObjectsCount.ToString());
for (int i = 0, n = blobs.Length; i < n; i++)
{
if (blobs.Length > 0)
{
bc.ExtractBlobsImage(numberplate, blobs[i], true);
Bitmap copy = blobs[i].Image.ToManagedImage();
pictureBox2.Image = numberplate;
pictureBox2.Refresh();
}
}
Bitmap managedImage = numer.ToManagedImage( );
Related
I'm generating spectogram out of many pixels. Problem is that it takes 0.8 second to render it. I would like to achieve that with threading so It doesn't freeze my GUI.
I'm very clueless. I tried many approaches but I can't get it working.
I need only this method to work and it should be fine.
volatile DrawingVisual dv = new DrawingVisual();
volatile RenderTargetBitmap rtb;
volatile DrawingContext dc;
private async Task<Image> GetSpectogram(List<int[]> Lines)
{
using (dc = dv.RenderOpen())
{
int y = -1;
int x = -1;
foreach (int[] list in Lines)
{
x += 1;
for (int i = 0; i < list.Length; i++)
{
y += 1;
int value = list[i];
dc.DrawRectangle(GetRGBFromValue(value), null, new Rect(x, y, 1, 1));
}
y = -1;
}
dc.Close();
}
await Task.Factory.StartNew(() => {
rtb = new RenderTargetBitmap(TotalLines, TotalPixelsInLine, 0, 0, PixelFormats.Default);
rtb.Render(dv);
Image img = new Image();
img.Source = rtb;
img.Height = rtb.Height * 3;
img.VerticalAlignment = VerticalAlignment.Stretch;
img.Stretch = Stretch.Uniform;
return img;
});
return null;
}
Can you please tell me what is the problem here, I would like to understand it?
It is throwing threading error at rtb.Render(dv);
Thank you very much :)
The function GetBitMapColorMatrix is ā€¸supposed to decode the tiff image and return a bitmap that describes the tiff image but for some reason, I get this exception -> System.PlatformNotSupportedException: 'System.Drawing is not supported on this platform'. Any help would be appreciated.
public Color[][] GetBitMapColorMatrix(string bitmapFilePath)
{
Bitmap b1 = new Bitmap(bitmapFilePath);
int hight = b1.Height;
int width = b1.Width;
Color[][] colorMatrix = new Color[width][];
for (int i = 0; i < width; i++)
{
colorMatrix[i] = new Color[hight];
for (int j = 0; j < hight; j++)
{
colorMatrix[i][j] = b1.GetPixel(i, j);
}
}
return colorMatrix;
}
UWP does not use the full .net framework so the classes in System.Drawing are not available. Their are some classes like BitmapPallette and BitmapImage in the System.Media.Imaging namespace that could be useful. I would also look at Win2D or SharpDX.
I am wondering how I could set an Image to "grid_Main", if I have created all my images in a loop (See Code).
Code:
private void MoleImageMaker()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
String MoleImageFunction = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
}
}
Something like this
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
Grid.SetColumn(mole, i);
grid_Main.Children.Add(mole);
}
I'm very new to visual C# I want to display an array of images in a picture box
Here's my code:
string[] list = Directory.GetFiles(#"C:\\pictures", "*.jpg");
Image[] images = new Image[5];
for (int index = 0; index < 5; index++)
{
//HERE IS WHERE IM STUCKED WITH
picturebox[index] = Image.FromFile(list[index]);
}
Edit-1 : This answer has a scope limited to Win-Forms C#.
You need certain assemblies added in your application before using this code.
using System.IO;
using System.Windows.Forms;
Edit ended;
Original Answer
You have to draw all image to one image for displaying them in single picturebox
That is bit complex you can use mutiple pictureboxes
In following code they are created dynamically according to need:
// For confirm visibility of all images set
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
if(index % 3 == 0)
y = y + 150; // 3 images per rows, first image will be at (20,150)
picturebox[index].Location=new Point(index * 120 + 20, y);
picturebox[index ].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
The answer provided throws an object reference exception. Otherwise thanks for the example!
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
should be
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
Use picturebox[index].Image = Image.FromFile(list[index]);
//this code help you to work with picturebox in arraye
public partial class Form_Begin : Form
{
PictureBox[] pictureBoxs = new PictureBox[50];
public Form_Begin()
{
InitializeComponent();
pictureBoxs[0] = pictureBox1;
pictureBoxs[1] = pictureBox2;
pictureBoxs[2] = pictureBox3;
pictureBoxs[3] = pictureBox4;}
List<PictureBox> pictureBoxes = new List<PictureBox>();
private void buttonX1_Click(object sender, EventArgs e)
{
for (int i = 0; i <2; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_1; // Load Image_1 from Resources on property of picturebox
}
for (int i = 2; i < 4; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_2; // Load Image_12 from Resources on property of picturebox
}
private void picbutton_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
PictureBox[] picture = new PictureBox[5];
int x = 0;
int y = 15;
for (int index = length; index < picture.Length; index++)
{
picture[index] = new PictureBox();
picture[index].Size = new Size(100, 50);
open.Title = "OPen Image";
open.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif";
DialogResult result = open.ShowDialog();
if (result == DialogResult.OK)
{
picture[index].BackgroundImage = new Bitmap(open.FileName);
picture[index].SizeMode = PictureBoxSizeMode.AutoSize;
listBox1.Controls.Add(picture[index]);
if ((x % 3 == 0) && (index != 0))
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picture[index].Location = new Point(x * 210 + 20, y);
picture[index].Size = new Size(200, 150);
x++;
}
}
}
I'm having trouble generating a QR code on mango 7.1 with ZXing 2.0.
It should be pretty straight forward, but it's not working.
The code:
QRCodeWriter writer = new QRCodeWriter();
var bMatrix = writer.encode("Hey dude, QR FTW!", BarcodeFormat.QR_CODE, 25, 25);
var asBitmap = bMatrix.ToBitmap();
image1.Source = asBitmap;
image1 comes from the xaml.
bMatrix seems to contain the data that I need, but image1 never shows a thing.
So I managed to do a workaround. I'm not sure if my original code didnt work due to a bug in the ZXing C# port or if I did something wrong. Anyhow, here is what I did to show the QR code.
image1 comes from xaml.
QRCodeWriter writer = new QRCodeWriter();
var bMatrix = writer.encode("Hey dude! QR FTW!", BarcodeFormat.QR_CODE, width, height);
WriteableBitmap wbmi = new System.Windows.Media.Imaging.WriteableBitmap(width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int grayValue = bMatrix.Array[y][x] & 0xff;
if (grayValue == 0)
wbmi.SetPixel(x, y, Color.FromArgb(255, 0, 0,0));
else
wbmi.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255));
}
}
image1.Source = wbmi;
Try setting the image source like this :
image1 = new ImageBrush { ImageSource = asBitmap ;}
I run into the same problem. Assigning the WriteableBitmap directly to Image.Source didn't work.
After some search I found a strengh Workaround which writes the WritableBitap into a MemoryStream using SaveJpeg method:
using (MemoryStream ms = new MemoryStream())
{
asBitmap.SaveJpeg(ms, (int)asBitmap.PixelWidth, (int)asBitmap.PixelHeight, 0, 100);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(ms);
Image.Source = bmp;
}
This worked unless the QR code was displayed in dark/light blue, not black/white. Telling this a friend he remebered that in Windows phone pixel Color is not a Byte, but an integer. With this knowledge and the sources of zxing I changed the ByteMatrix.ToBitmap method as follows:
public WriteableBitmap ToBitmap()
{
const int BLACK = 0;
const int WHITE = -1;
sbyte[][] array = Array;
int width = Width;
int height = Height;
var pixels = new byte[width*height];
var bmp = new WriteableBitmap(width, height);
for (int y = 0; y < height; y++)
{
int offset = y*width;
for (int x = 0; x < width; x++)
{
int c = array[y][x] == 0 ? BLACK : WHITE;
bmp.SetPixel(x, y, c);
}
}
//Return the bitmap
return bmp;
}
And this solved the problem at all, even assigning the WritableBitmap directly to Image.Source. It seemed, the Image was correctly assigned, but the alpha value was transparent, which was removed when creating a jpeg.
The easiest solution:
Uri uri = new Uri("http://www.esponce.com/api/v3/generate?content=" + "your content here" + "&format=png");
image1.Source = new BitmapImage(uri);