Hello I need a help of Converting BitMatrix to bitmap . Here is my Code and I am using Zxing Library
var encodeHint = new Dictionary<EncodeHintType, object>();
String contents;
Bitmap bitmap = null;
QRCodeWriter writer = new QRCodeWriter();
encodeHint.Add(EncodeHintType.CHARACTER_SET, ENCORD_NAME);
encodeHint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BitMatrix bitData;
contents = ss;
bitData = writer.encode(contents, BarcodeFormat.QR_CODE,100,100, encodeHint);
Please Note that I am making a Joined or Linked Qr Code that is a special type QR code used in Japan . Reference is Linked QR Code . I could make QR code using BarcodeWriter but I have to use like this way . How to convert this BitMatrix to bitMap If I use
bitmap = bitData.ToBitmap();
I got here bitmap Null And The exception is "Found Empty Contents".
This example would be applicable.
It seems that Image can be obtained by passing BitMatrix to the Write method of the BarcodeWriter class.
File: Form1.cs Project: shuxp/test_QRCode
private Bitmap GenByZXingNet(string msg)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
...
...
ZXing.Common.BitMatrix bm = writer.Encode(msg);
Bitmap img = writer.Write(bm);
return img;
}
Related
I want to display QR code generated by QRCoder library ( https://github.com/codebude/QRCoder/ ) in my WinUI 3 desktop app.
From QRCoder I get System.Drawing.Bitmap object:
QRCodeGenerator qrCodeGenerator = new();
QRCodeData qrCodeData = qrCodeGenerator.CreateQrCode(associateSoftwareTokenResponse.SecretCode, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new(qrCodeData);
Bitmap qrCodeBitmap = qrCode.GetGraphic(20);
Then assigning it to XAML Image control: qrCodeImage.Source = qrCodeBitmap gives an error:
Error CS0029 Cannot implicitly convert type 'System.Drawing.Bitmap' to
'Microsoft.UI.Xaml.Media.ImageSource'
So apparently there is still some conversion needed.
All documentation and examples I managed to find explain how to print an image from file but not Bitmap object.
How can I display this code generated Bitmap in my WinUI 3 app?
You should be able to create a BitmapImage from a stream something like this:
Bitmap qrCodeBitmap = ...;
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream stream = new MemoryStream())
{
qrCodeBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
bitmapImage.SetSource(stream.AsRandomAccessStream());
}
image.Source = bitmapImage;
I need to convert a JPG image to PNG and change its white background to transparent instead. I am using ImageMagick.NET and I have found the following ImageMagick command that is supposed to do what I am trying to achieve:
convert image.jpg -fuzz XX% -transparent white result.png
I have tried converting this to c# but all I am getting is a png image with a white background. My code snippet:
using (var img = new MagickImage("image.jpg"))
{
img.Format = MagickFormat.Png;
img.BackgroundColor = MagickColors.White;
img.ColorFuzz = new Percentage(10);
img.BackgroundColor = MagickColors.None;
img.Write("image.png");
}
Any kind of help will be greatly appreciated. Thank you!!
This is a late response as it took me a while to find an answer myself, but this seems to work for me quite well. Look at where the Background property is assigned the Transparent value.
using (var magicImage = new MagickImage())
{
var magicReadSettings = new MagickReadSettings
{
Format = MagickFormat.Svg,
ColorSpace = ColorSpace.Transparent,
BackgroundColor = MagickColors.Transparent,
// increasing the Density here makes a larger and sharper output to PNG
Density = new Density(950, DensityUnit.PixelsPerInch)
};
magicImage.Read("someimage.svg", magicReadSettings);
magicImage.Format = MagickFormat.Png;
magicImage.Write("someimage.png");
}
In my case, I wanted to send this to UWP Image element, so instead of Write(), I did the following after the steps above:
// Create byte array that contains a png file
byte[] imageData = magicImage.ToByteArray();
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
{
writer.WriteBytes(imageData);
await writer.StoreAsync();
}
await bitmapImage.SetSourceAsync(stream);
}
return bitMapImage; // new BitMapImage() was scoped before all of this
Then on the UWP Image element, simply use:
imageElement.Source = bitMapImage;
Most of the arguments on the command line are either properties or method on the MagickImage class. Your command would translate to this:
using (var img = new MagickImage("image.jpg"))
{
// -fuzz XX%
img.ColorFuzz = new Percentage(10);
// -transparent white
img.Transparent(MagickColors.White);
img.Write("image.png");
}
Currrently, I am trying to get my face image from the database but I cannot get it. Its saying my I cannot convert System.Drawing.Image to EmguCV Gray Byte. Can I know whats my mistake on it. This is my first time I am using EmguCV.
//Eigen face recognizer
EigenObjectRecognizer recognizer = new
GetFaceFromDB(), //database
labels.ToArray(), //facename list
3000,
ref termCrit);
name = recognizer.Recognize(result);
This is my DB code:
private Image GetFaceFromDB()
{
Image FetchImg;
if (rowNumber >= 0)
{
byte[] FetchImgBytes = ((byte[])TSTable.Rows[rowNumber]["FaceImage"]);
System.IO.MemoryStream stream = new System.IO.MemoryStream(FetchImgBytes);
FetchImg = Image.FromStream(stream);
return FetchImg;
}
else
{
MessageBox.Show("No Image yet. Add image into database");
return null;
}
}
Thank you.
Well the problem is the conversion.
If you are using an image like this
Image<Gray,Byte> image = new Image<Gray,Byte>
initialized like this then you need to call image.Bitmap to get a System.Drawing.Image
If you are trying to do the reverse you should call something like this
Image<Gray, Byte> image = new Image<Gray, Byte>(BitmapImage);
Then your code should work correctly.
Hope this helps!
I'm trying to create a mini tool for my job. I'm using C# with Visual Studio 2010 to do that. Now I need to export an image to an act file.
Following here, http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmappalette.aspx
I wrote
class Palette : List<Color>
{
public static Palette GetFromImage(String PathToImage)
{
//Load imgae
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(PathToImage, UriKind.RelativeOrAbsolute);
image.EndInit();
//Load palette
BitmapPalette BitmapPalette = new BitmapPalette(image, 256);
//Convert it to list of Color
Palette pal = new Palette();
for (int i = 0; i < BitmapPalette.Colors.Count; i++)
{
pal.Add(Color.FromArgb(BitmapPalette.Colors[i].R, BitmapPalette.Colors[i].G, BitmapPalette.Colors[i].B));
}
//Return the palette has been creatted
return pal;
}
This code has effected, but the order of all colors in the result did not same as the order of all colors when I export by photoshop. So, when I write all colors to a act file, the act file is not correct.
Anybody can help me.
Many thank
Act file is compilated binary file.
You cannot encode an act file.
Perhaps Photoshop can import a css file (as like palet)
I am trying to convert the local HTML file to bitmap image using C# Windows Form Application. For that I am reading the HTML file by using memory stream. But after passing the memory stream to Bitmap object it saying "Parameter is not valid".
Below is the sample code
MemoryStream stm = new MemoryStream(data);
Bitmap f_Bitmap = (Bitmap)Image.FromStream(stm);
Please provide the solution for how can i covert the HTML file to bitmap image.
Thanks.
You can always use the WebBrowser object (read more here).
public void WBCapture()
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.ScrollBarsEnabled = true;
wb.Width = 800;
wb.Height = 600;
wb.DocumentText = #"<b>Hello</b> <i>World</i>!!!";
// Or you can navigate to:
// wb.Navigate("http://mydocmentsurl.com");
}
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
using (Graphics graphics = wb.CreateGraphics())
using (Bitmap bitmap = new Bitmap(wb.Width, wb.Height, graphics))
{
Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
wb.DrawToBitmap(bitmap, bounds);
bitmap.Save(#"C:\caputre.png", ImageFormat.Png);
}
}
My explanation is on the error your receiving :
The reason it is throwing error cause all you have done is pushed HTML text from the file in to the stream and when FromStream uses the data it says the format of the data is not a bitmap or good enough for it
If you want to save the contents of an HTML file as a Bitmap you cannot just put the bytes into a Bitmap object. You'll need to use a WebBrowser object and draw it manually. Here is an article that outlines it.
This will work as long as the HTML file you speak of is a direct link to the image file:
var request = (HttpWebRequest) WebRequest.Create(IMAGE_URL);
using (var stream = request.GetResponse().GetResponseStream())
{
using (var image = Image.FromStream(stream))
{
var bitmap = new Bitmap(image);
//use or return bitmap, image will automatically get disposed
}
}
Replace IMAGE_URL with your URL.
If the HTML file contains the URL as text, then you will need to parse it out.
If this file is local, you can simply use a FileStream (or FileReader) to get a stream of the local file (this is similar to GetResponseStream on a WebRequest.)
Let me know if you have questions.
Hope it helps.