How to display Bitmap object in WinUI 3 application - c#

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;

Related

Convert ZXing.Common.BitMatrix to bitmap Zxing in C#

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

How to convert stream to bitmap in xamarin.forms to adjust image contrast

I am trying to raise the contrast of an image in Xamarin.forms using this algorithm. My code gives me in image in the form of a System.IO.Stream, which I want to convert to a Bitmap/LockBitmap to use the algorithm.
I tried code such as using System.Drawing, which didn't work. The error said "the type or namespace bitmap could not be found." Then, I tried using Android.Graphics, which fixes that error, but I don't know if it will work with a Xamarin.forms app that needs to run on Android and iOS.
Even if that does work, how do I actually convert a System.IO.Stream into a Bitmap? Is there another algorithm that doesn't require bitmaps?
Edit: I think I can use BitmapFactory to convert the stream into a bitmap.
you could use DependencyService to convert System.IO.Stream into Bitmap ,after raise the contrast of an image ,return the new stream,and show the Image in forms page.
like:
create a interface IRaiseImage.cs:
public interface IRaiseImage
{
Stream RaiseImage(Stream stream);
}
then in Droid.project,creat AndroidRaiseImage.cs:
[assembly: Dependency(typeof(AndroidRaiseImage))]
namespace App18.Droid
{
class AndroidRaiseImage : IRaiseImage
{
public Stream RaiseImage(Stream stream)
{
Bitmap bitmap = BitmapFactory.DecodeStream(stream);
//raise the bitmap contrast
...
// return the new stream
MemoryStream ms = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms);
return ms;
}
}
}
then you could set to the Image in your forms page:
MemoryStream memoryStream = new MemoryStream();//your original image stream
Image image = new Image();
image.Source = ImageSource.FromStream(() => DependencyService.Get<IRaiseImage>().RaiseImage(memoryStream));

How to convert BitmapImage to StdPicture

I have a BitmapImage object loaded with an icon, I need to convert that to StdPicture. How do I do that ?
I found tons of examples on how to convert System.Drawing.Image to StdPicture but nothing for System.Windows.Media.Imaging.BitmapImage to StdPicture.
Do I have to first convert it to Winforms Image and then to StdPicture ? Sounds a bit silly to me.
I am assuming stdPicture you mean Bitmap.. So here we go.
private Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
using (MemoryStream os = new MemoryStream())
{
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
encoder.Save(os);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(os);
return new Bitmap(bitmap);
}
}

Silverlight 4: How can I convert bmp byte array to png byte array?

I have a wcf service which returns a bmp in byte[]. However Silverlight's Image control doesnt support displaying bmp's so i need to convert the bmp byte[] to png or jpg byte[]. Is there a library out there which does this conversion? Or any other way of displaying the bmp byte[] on the silverlight client?
Thanks!
Update1
In order to achieve the conversion I would have done something like this in .NET
private byte[] ConvertBmpToJpeg(byte[] bmp)
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bmp)))
{
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
}
Since System.Drawing is not available in Silverlight, how do I achieve what the code does above in Silverlight?
Answer
using the library mentioned by dj kraze below-
ExtendedImage img = new ExtendedImage();
var bd = new BmpDecoder();
var je = new JpegEncoder();
bd.Decode(img, new MemoryStream(bitmapBytes));
MemoryStream ms = new MemoryStream();
je.Encode(img, ms);
BitmapImage bi = new BitmapImage();
bi.SetSource(new MemoryStream(ms.ToArray()));
display_ScreenShot.Source = bi;
Here is an even easier way of doing it..
This site may help out a lot
Image Converting

Silverlight 4 : Converting image into byte[]

I have found how to do this in .NET 4.0, but I think JpegBitmapEncoder doesn't exist in Silverlight:
MemoryStream memStream = new MemoryStream();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageC));
encoder.Save(memStream);
var bytes = memStream.GetBuffer();
How can I convert an image to bytes[] in silverlight?
UPDATE:
I have a Contact model, which has a Photo property. Whenever I add a new Contact, I would like to load a local default Image and convert it and set the Photo property to it.
var bitmapImage = new BitmapImage
{
UriSource = new Uri("pack://application:,,,/xxx;component/Images/default.JPG")
};
var image = new Image{Source = bitmapImage};
Is this the correct way to load an image in first place?
Use
myImage.Save(memStream, ImageFormat.Jpeg);
return memStream.ToArray();
UPDATE
OK it turns out that the image is a BitmapImage.
It seems that BitmapImage does not expose the functionality to save the image. The solution is to get the image from the embedded resource:
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
Have a look at this library: Imagetools
It contains some nice utilities and jpg and png encoders,

Categories