Cod de bare in JPEG

Lasă un comentariu

7 Ianuarie 2015 de liviustoica55


public
static
str getBarcodeJpegImageAsBase64String(str barcodeStr, str fontName, int fontSize)

{

System.Drawing.Bitmap barcode;

System.Drawing.SizeF dataSize;

System.Drawing.Font barcodeFont;

System.Drawing.Graphics graphics;

System.IO.MemoryStream memoryStream;

System.String base64Image;

 


// Surrounding by *’s tells the barcode to render

barcodeStr = System.String::Format(„*{0}*”, barcodeStr);

barcode = new System.Drawing.Bitmap(1, 1);

barcodeFont = new System.Drawing.Font(fontName, fontSize, System.Drawing.FontStyle::Regular, System.Drawing.GraphicsUnit::Point);

 


// Get graphics object from the bitmap

graphics = System.Drawing.Graphics::FromImage(barcode);

 


// measure out the size of the string when rendered as a barcode

dataSize = graphics.MeasureString(barcodeStr, barcodeFont);

 


// Updating the image to the new size

barcode = new System.Drawing.Bitmap(barcode, dataSize.ToSize());

 


// Updating the graphics with the current size

graphics = System.Drawing.Graphics::FromImage(barcode);

 

graphics.Clear(System.Drawing.Color::get_White());

 


//Smoother rendering for scanning

graphics.set_TextRenderingHint(System.Drawing.Text.TextRenderingHint::SingleBitPerPixel);

 


//Draw out the barcode

graphics.DrawString(barcodeStr, barcodeFont, new System.Drawing.SolidBrush(System.Drawing.Color::get_Black()), 0, 0);

 


// Save the barcode to a base 64 string

memoryStream = new System.IO.MemoryStream();

barcode.Save(memoryStream, System.Drawing.Imaging.ImageFormat::get_Jpeg());

base64Image = System.Convert::ToBase64String(memoryStream.ToArray());

 


// Clean up

graphics.Flush();

barcodeFont.Dispose();

graphics.Dispose();

memoryStream.Dispose();

 


return base64Image;

}

Lasă un răspuns

Completează mai jos detaliile despre tine sau dă clic pe un icon pentru autentificare:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s

%d blogeri au apreciat asta: