Files
17168ERP/web/App_Script/barcode/generator.ashx
2025-08-29 01:27:25 +08:00

106 lines
3.1 KiB
Plaintext

<%@ WebHandler Language="C#" Class="generator" %>
using System;
using System.Web;
using System.IO;
using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;
public class generator : IHttpHandler {
MyWeb.function f = new MyWeb.function();
public void ProcessRequest (HttpContext context) {
if (!f.isStrNull(context.Request["val"]))
{
try
{
MemoryStream oStream = new MemoryStream();
CreateValidateCodeImage(ref oStream);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(oStream.ToArray());
}
catch (Exception ex)
{
context.Response.ContentType = "text/plain";
context.Response.Write(ex.Message);
}
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("No data");
}
}
public void CreateValidateCodeImage(ref MemoryStream MemoryStream)
{
Bitmap oBmp = CreateValidateCodeImage();
try
{
oBmp.Save(MemoryStream, ImageFormat.Gif);
}
catch (Exception)
{
}
finally
{
oBmp.Dispose();
}
}
public Bitmap CreateValidateCodeImage()
{
//設定字型
int FontSize = 10;
if ( !f.isStrNull(HttpContext.Current.Request["fontsize"]) && f.IsNumeric(HttpContext.Current.Request["fontsize"]))
FontSize = f.Val(HttpContext.Current.Request["fontsize"]);
Font oFont = new Font("Times New Roman", FontSize, FontStyle.Regular);
Barcode bc = new Barcode();
bc.IncludeLabel = true;
bc.Alignment = AlignmentPositions.CENTER;
bc.Width =!f.isStrNull(HttpContext.Current.Request["w"]) && f.IsNumeric(HttpContext.Current.Request["w"])?f.Val(HttpContext.Current.Request["w"]):400;
bc.Height =!f.isStrNull(HttpContext.Current.Request["h"]) && f.IsNumeric(HttpContext.Current.Request["h"])?f.Val(HttpContext.Current.Request["h"]):200;
bc.RotateFlipType = RotateFlipType.RotateNoneFlipNone;
bc.LabelFont = oFont;
bc.BackColor = Color.White;
bc.ForeColor = Color.Black;
string data = f.ValString(HttpContext.Current.Request["val"]);
string type = f.ValString(HttpContext.Current.Request["type"]);
if (type == "39")
return (Bitmap)bc.Encode(TYPE.CODE39, data);
else if (type == "128")
return (Bitmap)bc.Encode(TYPE.CODE128, data);
else if (type == "ISBN")
return (Bitmap)bc.Encode(TYPE.ISBN, data);
else if (type == "ITF")
return (Bitmap)bc.Encode(TYPE.ITF14, data);
else if (type == "EAN13")
return (Bitmap)bc.Encode(TYPE.EAN13, data);
else if (type == "EAN8")
return (Bitmap)bc.Encode(TYPE.EAN8, data);
else
return (Bitmap)bc.Encode(TYPE.CODE39, data);
}
public bool IsReusable {
get {
return false;
}
}
}