Can't convert tiff image to a bitmap (matrix) - c#

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.

Related

Windows.Forms - Change ImageList Bitmaps pixels for HighContrast

Need help with altering the Bitmaps in an ImageList to switch the black pixels to yellow, they are being used as images for a UICommandBar (screenshot and attempt below)
The code gets executed, with GetPixel condition and then SetPixel, but the images are not changed. Maybe related to ImageListStreamer or not the right time to swap those pixels.
rtfeditor.cs
this.imageList1.ImageStream = ( (System.Windows.Forms.ImageListStreamer)(resources.GetObject( "imageList1.ImageStream" ) ) );
this.imageList1 = ColorSpace.ProcessHighContrast(this.imageList1);
Utils\ColorSpace.cs
public static System.Windows.Forms.ImageList ProcessHighContrast(System.Windows.Forms.ImageList imageList)
{
if (System.Windows.Forms.SystemInformation.HighContrast)
{
foreach (System.Drawing.Bitmap imageListImage in imageList.Images)
{
for (int i = 0; i < imageListImage.Width; i++)
{
for (int j = 0; j < imageListImage.Height; j++)
{
Color color = imageListImage.GetPixel(i, j);
if (System.Drawing.Color.FromArgb(255, 0, 0, 0).Equals(color))
imageListImage.SetPixel(i, j, System.Drawing.SystemColors.WindowText);
}
}
}
}
return imageList;
}
Solved
Hans Passant's comment was critical for resolving this (see quoted comment below) + changing the order of the call of ProcessHighContrast (now first) with the assignment of the ImageList to the Janus.Windows.UI.CommandBars.UICommandBar (now second). New method and calling code below.
ImageList.Images returns a copy of each image. Altering the copy therefore does not modify the ImageList content at all. Create a new ImageList instead. Do consider that Darth Vader color schemes tend to be appreciated only by programmers, regular users expect the high contrast version to have a white background. That's going to be a lot less painful. – Hans Passant
rtfeditor.cs
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1 = ColorSpace.ProcessHighContrast(this.imageList1);
this.uiCommandManager1.ImageList = this.imageList1;
Utils\ColorSpace.cs
public static System.Windows.Forms.ImageList ProcessHighContrast(System.Windows.Forms.ImageList imageList)
{
if (System.Windows.Forms.SystemInformation.HighContrast)
{
System.Windows.Forms.ImageList imageListNew = new ImageList();
foreach (System.Drawing.Bitmap imageListImage in imageList.Images)
{
for (int i = 0; i < imageListImage.Width; i++)
{
for (int j = 0; j < imageListImage.Height; j++)
{
Color color = imageListImage.GetPixel(i, j);
if (System.Drawing.Color.FromArgb(255, 0, 0, 0).Equals(color))
imageListImage.SetPixel(i, j, System.Drawing.SystemColors.WindowText);
}
}
imageListNew.Images.Add(imageListImage);
}
return imageListNew;
}
return imageList;
}

Remove camera distortion from part of image in C# using EmguCV

I am trying to remove distortion from a cropped version of my image. I know the x and y mapping matrixes for the full size image (1920X1080). The way I am trying to do this is to first create two new mappings that contain the part of the full size mappings corresponding to the crop rectangle and then apply them to the cropped image. However, the output is just a black image:
Matrix<float> matCam1Map1, matCam1Map2;
Matrix<float> matCam1Map1Crp, matCam1Map2Crp;
matCam1Map1 = new Matrix<float>(1080, 1920);
matCam1Map2 = new Matrix<float>(1080, 1920);
CvInvoke.cvInitUndistortMap(itsCam1.IntrinsicMatrix.Ptr, itsCam1.DistortionCoeffs.Ptr, matCam1Map1.Ptr, matCam1Map2.Ptr);
matCam1Map1Crp = new Matrix<float>(rectCam1Crop.Height, rectCam1Crop.Width);
matCam1Map2Crp = new Matrix<float>(rectCam1Crop.Height, rectCam1Crop.Width);
for (int i = 0; i < rectCam1Crop.Height; i++)
{
for (int j = 0; j < rectCam1Crop.Width; j++)
{
matCam1Map1Crp.Data[i, j] = matCam1Map1.Data[rectCam1Crop.Y + i, rectCam1Crop.X + j];
matCam1Map2Crp.Data[i, j] = matCam1Map2.Data[rectCam1Crop.Y + i, rectCam1Crop.X + j];
}
}
Capture capCam1;
Image<Bgr, Byte> imgCam1New = capCam1.QueryFrame().Copy(rectCam1Crop);
Image<Bgr, Byte> imgNoDistortion = new Image<Bgr, byte>(imgCam1New.Size);
CvInvoke.cvRemap(imgCam1New, imgNoDistortion, matCam1Map1Crp, matCam1Map2Crp, 0, new MCvScalar(0));
When I apply the full size mappings to the full size image, it works perfectly, but then I have to crop it. However, I am trying to make it faster by cropping it first, and then applying the mappings to the cropped image.
Does anyone know what may be the issue with my code?
Thanks

transparent component with aviwriter in aforge

I want to make an avi video with aforge and I am trying to add bitmap frame with ARGB the A component (transparent component) is not working please is there any way to add transparent to aviwriter frame with aforge.net c# (even if not bitmap)
Bitmap my_video_image = new Bitmap(picturebox1.Image);
for (int i = 0; i < heigth11; i++)
{
for (int j = 0; j < width11; j++)
{
my_video_image.SetPixel(j,i,Color.FromArgb(trackBar4.Value,red,green, blue));
}
}
AVIWriter avi_Writer= new AVIWriter();
avi_Writer.Open("videoname.avi",600,600);
avi_Writer.AddFrame(my_video_image);
the avi WRiter added the image but without the tranparent component

How to convert an Unmanaged Image to a managed bitmap image?

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

Generating QR code on Windows Phone 7.1 using ZXing 2.0 C# Port

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

Categories

Resources