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;
Related
I am working on a QR Code project and one of the requirements is to be able to download the generated QR Code in SVG format. I was able to display the generated QR Code in PNG format, but I have been trying to download it in SVG format.
I am using XZing library to generate the QR Code in PNG
Color barcodeColor = System.Drawing.ColorTranslator.FromHtml(colorHex);
BarcodeWriter barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Width = width,
Height = height,
PureBarcode = true
},
Renderer = new BitmapRenderer
{
Foreground = barcodeColor
}
};
Bitmap barCodeBitmap = barcodeWriter.Write(qrContent);
var memoryStream = new MemoryStream();
// save to stream as PNG
barCodeBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(memoryStream.ToArray());
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
response.StatusCode = HttpStatusCode.OK;
return response;
I was able to create a BarcodeWriterSvg object but I wasn't able to convert it into a stream and pass it in the header like the PNG
BarcodeWriterSvg barcodeWriter = new BarcodeWriterSvg
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Width = width,
Height = height,
PureBarcode = true
},
Renderer = new SvgRenderer
{
Foreground = barcodeColor
}
};
var barCodeBitmap = barcodeWriter.Write(qrContent);
var memoryStream = new MemoryStream();
HttpResponseMessage response = new HttpResponseMessage();
...
My question is how to be able to convert the object into a stream and pass it in the header? The ultimate goal is to be able to download the file without actually creating a physical copy.
Off the top of my head, and attempting to use the same code paradigm from your example, I would look into something like this:
var barcodeWriter = new BarcodeWriterSvg();
....
....
var svgImage = barcodeWriter.Write(text); //ZXing.Rendering.SvgRenderer.SvgImage
var response = new HttpResponseMessage();
response.Content = new StringContent(svgImage.Content);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/svg+xml");
return response;
I have been trying to set a bitmap as cover art for a MP3 but I can't seem to get it working. It isn't throwing any errors but when I play the MP3 the bitmap isn't showing.
This is what I currently have:
TagLib.File f = TagLib.File.Create("song.mp3");
Image currentImage = getAlbumArt(result.passedAlbumID);
Picture pic = new Picture();
pic.Type = PictureType.FrontCover;
pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
pic.Description = "Cover";
MemoryStream ms = new MemoryStream();
currentImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
pic.Data = ByteVector.FromStream(ms);
f.Tag.Pictures = new IPicture[1] { pic };
pictureBox1.Image = currentImage; //testing the image is correct
f.Save();
ms.Close();
I'm using the following code and everything works fine for me:
TagLib.File file = TagLib.File.Create(/*path to your mp3 file*/);
TagLib.Picture pic = new TagLib.Picture();
pic.Type = TagLib.PictureType.FrontCover;
pic.Description = "Cover";
pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
MemoryStream ms = new MemoryStream();
/*your image*/.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
pic.Data = TagLib.ByteVector.FromStream(ms);
file.Tag.Pictures = new TagLib.IPicture[] { pic };
file.Save();
ms.Close();
According to your provided code, the only thing I noticed is, that my code is using following line
file.Tag.Pictures = new TagLib.IPicture[] { pic };
instead of
f.Tag.Pictures = new TagLib.IPicture[1] { pic };
So simply try, if it works when you remove the 1 inside the square brackets.
I'm tryng to export images from docx file.
How can I convert EncodedPackage from Shape object as image?
Sample of my code:
DocumentFormat.OpenXml.Vml.Shape shape = imageElement.Descendants<DocumentFormat.OpenXml.Vml.Shape>().FirstOrDefault();
byte[] bytes = System.Convert.FromBase64String(shape.EncodedPackage.Value.Replace("\n", ""));
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
}
image.Save(fileName);
You can get the image from ImagePart. Try this:
var imageParts = doc.MainDocumentPart.ImageParts;
foreach (var imagePart in imageParts)
{
var uri = imagePart.Uri;
var filename = uri.ToString().Split('/').Last();
var stream = doc.Package.GetPart(uri).GetStream();
Bitmap b = new Bitmap(stream);
b.Save(#"C:\Extracted_Images\" + filename);
}
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.
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;
}