Why cant I use Gma.QrCodeNet.Encoding.Windows.Render? - c#

I'm using QrCode.Net library version 0.3 and I need to use Gma.QrCodeNet.Encoding.Windows.Render in order to create images with qrcode ISizeCalculation but I'm missing somethig or there's another version outhere. What can be the problem?
Anyway I found a solution for people with the same problem and they wanna create images with the same fixed size. Here is the code:
private void gen_qr_file(string file_name, string content, int image_size)
{
string new_file_name = file_name;
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(content, out qrCode);
Renderer renderer = new Renderer(image_size, Brushes.Black, Brushes.White);
MemoryStream ms = new MemoryStream();
renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);
var image = new Bitmap(Image.FromStream(ms), new Size(new Point(200, 200)));
image.Save(new_file_name + ".png", ImageFormat.Png);
}
This generate a png image of 200x200 pixels with the qrcode.
The library itself has a method to do this, but I need to include the RENDER thing and I can't. Someone knows what's the problem?

private void gen_qr_file(string file_name, string content, int image_size) {
string new_file_name = file_name;
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(content, out qrCode);
Renderer renderer = new Renderer(image_size, Brushes.Black, Brushes.White);
MemoryStream ms = new MemoryStream();
renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);
var imageTemp = new Bitmap(ms);
var image = new Bitmap(imageTemp, new Size(new Point(image_size, image_size)));
image.Save(new_file_name + ".png", ImageFormat.Png);
}
Note: Only 2 lines are modified. I hope it helps somebody.

Use FixedCodeSize. See example below which will produce a 400x400px image, with each 'module' (block) getting smaller the more data is added.
var qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
var qrCode = qrEncoder.Encode("my value");
var renderer = new GraphicsRenderer(new FixedCodeSize(400, QuietZoneModules.Zero), Brushes.Black, Brushes.White);
renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, /* OUTPUT STREAM */);

I had to include both of the following statements:
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Controls;
Also, how big is Gma.QrCodeNet.Encoding.dll ?
It should be over 80K or you have the wrong one.
I had the same issue.

This is my implementation Only change GraphicsRenderer
private string gen_qr_file(string file_name, string content, int image_size)
{
string new_file_name = file_name;
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(content, out qrCode);
GraphicsRenderer renderer = new GraphicsRenderer(
new FixedCodeSize(400, QuietZoneModules.Zero),
Brushes.Black,
Brushes.White);
MemoryStream ms = new MemoryStream();
renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
var imageTemp = new Bitmap(ms);
var image = new Bitmap(imageTemp, new Size(new Point(200, 200)));
image.Save(new_file_name, ImageFormat.Png);
return new_file_name;
}

Related

Load generated image into asp image control wihtout saving it physically

I'm trying to load an image into an asp image control.
That image is generated from zxing Barcode Writter.
My question is, can I load it wihtout physically saving it first?
string barcode = "xxxxxx";
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128 };
imgBarCode.ImageUrl = writer.Write(barcode);
... How can I reference writer.Writer to image control "imgBarCode"
With the suggestion made by user1429080 I ended up with this:
string barcode = "12345"
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128, Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 300 } };
var bitmap = writer.Write(barcode);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
var b64 = Convert.ToBase64String(ms.ToArray());
imgBarCode.ImageUrl = "data:image/jpeg;base64," + b64;

Implement Barcode in MVC

I want to Implement Barcode in MVC web App hosted on IIS, for Super Store. After 3 day research i found an example on google but that was not in MVC. Please tell me how can i Implement this code in MVC, so when i pass a string barcode image display in my view
public void CreateBarcode(string code)
{
var myBitmap = new Bitmap(500, 50);
var g = Graphics.FromImage(myBitmap);
var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
g.Clear(Color.White);
var strFormat = new StringFormat { Alignment = StringAlignment.Center };
g.DrawString(code, new Font("Free 3 of 9", 50), Brushes.Black, new RectangleF(0, 0, 500, 50), strFormat);
var myEncoder = System.Drawing.Imaging.Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(#"E:\Barcode.jpg", jgpEncoder, myEncoderParameters);
}
public ImageCodecInfo GetEncoder(ImageFormat format)
{
var codecs = ImageCodecInfo.GetImageDecoders();
foreach (var codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
It looks like you know how to make a barcode and need to know how to get it from a controller to your page. Here is a little code that may help you put it all together.
in your page
<img src="#Url.Action("Barcode", new { code = "*ABC123*" })" alt="" />
in your controller
public ActionResult Barcode(string code)
{
var myBitmap = new Bitmap(500, 50);
var g = Graphics.FromImage(myBitmap);
// the code that makes the barcode
// the code that makes the barcode
// the code that makes the barcode
// Put the image into a stream to return
MemoryStream ms = new MemoryStream();
myBitmap.Save(ms, ImageFormat.Png);
// Reset the stream position before returning it
ms.Position = 0;
return new FileStreamResult(ms, "image/png");
}

i have an issue in debugging and i don't know whats is the reason C#

i have two images the first one for product , and the second one for Barcode of product .
i use to create product this code
private void button1_Click(object sender, EventArgs e)
{
string barcode = CMBIDCAT.Text+"345" + TXTIDP.Text +"012" ;
Bitmap bitmap = new Bitmap(barcode.Length * 20, 50);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font ofont = new Font("code 128", 30);
PointF point = new PointF(2f, 2f);
SolidBrush blackbursh = new SolidBrush(Color.Black);
SolidBrush whitebursh = new SolidBrush(Color.White);
graphics.FillRectangle(whitebursh, 0, 0, bitmap.Width, bitmap.Height);
graphics.DrawString("*" + barcode + "*",ofont, blackbursh, point);
}
using (DataTable dt = new DataTable())
{
PIC2.Image = bitmap;
PIC2.Height = bitmap.Height;
PIC2.Width = bitmap.Width;
}
private void button3_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
PIC1.Image.Save(ms, PIC1.Image.RawFormat);
byte[] byteImage = ms.ToArray();
MemoryStream st = new MemoryStream();
PIC2.Image.Save(st, PIC2.Image.RawFormat);
byte[] byteImage1 = st.ToArray();
prd.ADD_PRODUCT(Convert.ToInt32(CMBIDCAT.SelectedValue), TXTIDP.Text, TXTNMP.Text, Convert.ToInt32(TXTFP.Text), Convert.ToInt32(TXTSP.Text), Convert.ToInt32(TXTTP.Text), TXTDES.Text, Convert.ToInt32(TXTQTE.Text), byteImage, byteImage1);
MessageBox.Show("تمت الاضافة بنجاح ", "عملية الاضافة",MessageBoxButtons.OK , MessageBoxIcon.Information);
}
i don't know the reason for this issue please any one help me
how can i solve this please .
I don't have the rep to just comment. But, should you be using a new MemoryStream for your second image?
According to the MSDN you should not write to a stream that has been written to.
MSDN "Image.Save Method (Stream, ImageFormat)"
Change your code to:
MemoryStream ms = new MemoryStream();
PIC1.Image.Save(ms, PIC1.Image.RawFormat);
byte[] byteImage = ms.ToArray();
MemoryStream ms1 = new MemoryStream();
PIC2.Image.Save(ms1, PIC2.Image.RawFormat);
byte[] byteImage1 = ms1.ToArray();
Hopefully that works. :)
Make sure which value is null while debugging whether the Format or the stream, and consider using the "using" statement with memory stream in order for the resources to be freed like the following :
byte[] byteImage1;
using(MemoryStream ms = new MemoryStream())
{
PIC1.Image.Save(ms, PIC1.Image.RawFormat);
byteImage1 = ms.ToArray();
}
And consider using two memory streams for each image as #Tim.E suggested.

How to use a font from a file in SharpDX?

i want to generate image for windows 8 app, i'm going to use SharpDX API
here is the code sample which i thankfully coped and paste
private MemoryStream RenderStaticTextToBitmap()
{
var width = 400;
var height = 100;
var pixelFormat = WicPixelFormat.Format32bppBGR;
var wicFactory = new ImagingFactory();
var dddFactory = new SharpDX.Direct2D1.Factory();
var dwFactory = new SharpDX.DirectWrite.Factory();
var wicBitmap = new Bitmap(
wicFactory,
width,
height,
pixelFormat,
BitmapCreateCacheOption.CacheOnLoad);
var renderTargetProperties = new RenderTargetProperties(
RenderTargetType.Default,
new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown),
0,
0,
RenderTargetUsage.None,
FeatureLevel.Level_DEFAULT);
var renderTarget = new WicRenderTarget(
dddFactory,
wicBitmap,
renderTargetProperties)
{
TextAntialiasMode = TextAntialiasMode.Cleartype
};
renderTarget.BeginDraw();
var textFormat = new TextFormat(dwFactory, "Consolas", 48)
{
TextAlignment = SharpDX.DirectWrite.TextAlignment.Center,
ParagraphAlignment = ParagraphAlignment.Center
};
var textBrush = new SharpDX.Direct2D1.SolidColorBrush(
renderTarget,
SharpDX.Colors.Blue);
renderTarget.Clear(Colors.White);
renderTarget.DrawText(
"Hi, mom!",
textFormat,
new RectangleF(0, 0, width, height),
textBrush);
renderTarget.EndDraw();
var ms = new MemoryStream();
var stream = new WICStream(
wicFactory,
ms);
var encoder = new PngBitmapEncoder(wicFactory);
encoder.Initialize(stream);
var frameEncoder = new BitmapFrameEncode(encoder);
frameEncoder.Initialize();
frameEncoder.SetSize(width, height);
frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare;
frameEncoder.WriteSource(wicBitmap);
frameEncoder.Commit();
encoder.Commit();
frameEncoder.Dispose();
encoder.Dispose();
stream.Dispose();
ms.Position = 0;
return ms;
}
this working in excellent way with installed fonts .... i have font in the assets folder and i want to use -i have about 604 custom fonts and i chose the font dynamically- , i know there is away to load file from folder .... help plz
Unfortunately, afaik, there is no API in DirectWrite that supports this easily. You need to develop your own font loader and related classes. There is the SharpDX sample CustomFont that is loading fonts from resources, so you could adapt it to load from another location.

Converting image from project folder

I am trying to convert a image in my image folder and the image name defaultImage and update into my database table.
But now I am having problem in this line of code:
I change the code using this:
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
instead of this:
System.Drawing.Image uploaded = System.Drawing.Image.FromStream(~/images/defaultImage);
I now getting the error of FileNotFoundException was unhandled by user code
I have tried this method using fileupload control and it working fine but not sure how to convert image in a folder.
How do I get the image from the folder in order to convert it using the method shown below.
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
//System.Drawing.Image uploaded = System.Drawing.Image.FromStream();
System.Drawing.Image newImage = new Bitmap(1024, 768);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(uploaded, 0, 0, 1024, 768);
}
byte[] results;
using (MemoryStream ms = new MemoryStream())
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters jpegParms = new EncoderParameters(1);
jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
newImage.Save(ms, codec, jpegParms);
results = ms.ToArray();
}
string sqlImage = "update MemberReport set image1 = #Data where memberreportid = '" + Session["memberreportid"] + "'";
SqlCommand cmdImage = new SqlCommand(sqlImage);
cmdImage.Parameters.AddWithValue("#Data", results);
InsertUpdateData(cmdImage);
I guess your problem is with relative paths, instead of
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
you should provide the local path, which you can get it this way:
Image uploaded6 = Image.FromFile(Server.MapPath("~/image/defaultImage.jpg"));
System.Drawing.Image.FromStream requires 'MemoryStream' as parameter.
byte[] file = null;
MemoryStream memoryStream = new MemoryStream();
memoryStream = new MemoryStream(file, false);
System.Drawing.Image objTempImg = System.Drawing.Image.FromStream(memoryStream)
byte[] file is based64 image
System.Drawing.Image.FromStream(Stream)
You must to send a Stream parameter in this method.

Categories