Barcode条形码及对图片进行270翻转
来源:互联网 发布:python使用指南 编辑:程序博客网 时间:2024/06/08 04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using Neodynamic.WebControls.BarcodeProfessional;
using System.IO;
namespace WebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetImage();
byte[] code = GetUpDownImage("888.png");
if (code != null)
{
Response.Clear();
Response.ContentType = "image/png";
Response.BinaryWrite(code);
Response.End();
}
else
{
Response.End();
}
}
protected void btn_Click(object sender, EventArgs e)
{
Response.Write(txt.Text);
}
protected void GetImage()
{
//生成条码
BarcodeProfessional bcp = new BarcodeProfessional(); //BarcodeProfessional第三方程序集
bcp.Code = "W002054965001";
bcp.Symbology = Symbology.Code39;
byte[] barcodeImage = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
//if (barcodeImage != null)
//{
// Response.Clear();
// Response.ContentType = "image/gif";
// Response.BinaryWrite(barcodeImage);
// Response.End();
//}
//else
//{
// Response.End();
//}
//把条码转换为Bitmap对象并保存
Bitmap img = BytesToBitmap(barcodeImage);
string imgPath = @"E:/HXWorkProject/WorkProject/WebApplication/WebApplication/Image";
Random rand = new Random();
string strImg = "";
if (Directory.Exists(imgPath))
{
strImg = ((rand.Next(100, 999)) + ".png");
img.Save(imgPath + "//" + strImg);
}
else
{
Directory.CreateDirectory(imgPath);
strImg = ((rand.Next(100, 999)) + ".png");
img.Save(imgPath + "//" + strImg);
}
}
protected byte[] GetUpDownImage(string FileName)
{
// Bitmap uses System.Drawing namespace.
Bitmap bmp = new Bitmap(Server.MapPath("image/" + FileName));
bmp.RotateFlip(RotateFlipType.Rotate270FlipX); //反转
// Size the _bmp to original bitmap's dimension.
Bitmap _bmp = new Bitmap(bmp.Width, bmp.Height);
// Uses temp _bmp to write on canvas.
Graphics canvas = Graphics.FromImage(_bmp);
// Draw the original indexed bitmap's content to the temp _bmp.
// Paint the entire region of original bitmap to the temp _bmp.
// Use the rectangle type to select area of source image.
canvas.DrawImage(bmp, new Rectangle(0, 0, _bmp.Width, _bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
// Make temp _bmp to general bmp.
bmp = _bmp;
// Make a new bitmap for Upside-down.
Bitmap bmpUpsideDown = new Bitmap(bmp.Width, bmp.Height);
int r = 0, g = 0, b = 0; // R,G,B for Original bmp's RGB value.
int avgY = (0 + bmp.Height) / 2; // avgY for average Y-axle value.
int tarY = 0; // tarY for Upside-down Y-axle value.
// Pixel by pixcel image processing.
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
// Get RGB from original bitmap.
r = bmp.GetPixel(x, y).R;
g = bmp.GetPixel(x, y).G;
b = bmp.GetPixel(x, y).B;
// Cause (Origal y + Target y) /2 = Average y.
tarY = 2 * avgY - y;
// Write to new Upsite-down bitmap on specified pixel and RGB.
if (bmp.Height % 2 == 0)
{
bmpUpsideDown.SetPixel(x, tarY - 1, Color.FromArgb(r, g, b));
}
else
{
bmpUpsideDown.SetPixel(x, tarY, Color.FromArgb(r, g, b));
}
}
}
// Specify HTML's content type.
//Response.Clear();
//Response.ContentType = "image/png";
// ImageFormat uses System.Drawing.Imaging namespace.
// Must use ImageFormat.Jpeg. If use ImageFormat.Bmp,
// you'll get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." error.
//bmpUpsideDown.Save(Response.OutputStream, ImageFormat.Jpeg);
bmpUpsideDown.Save(Server.MapPath("image/888-1.png"));
// You should always call the Dispose method to release
// the Graphics and related resources created by the
// FromImage method.
_bmp.Dispose();
bmp.Dispose();
bmpUpsideDown.Dispose();
canvas.Dispose();
try
{
FileInfo fileInfo = new FileInfo(Server.MapPath("image/888-1.png")); //strPath为文件路径
Stream fileStream = fileInfo.OpenRead();
int fileLength = Convert.ToInt32(fileStream.Length);
byte[] btContent = new byte[fileLength];
fileStream.Read(btContent, 0, fileLength);
fileStream.Close();
if (File.Exists(Server.MapPath("image/888-1.png")))
{
File.Delete(Server.MapPath("image/888-1.png"));
}
return btContent;
}
catch
{
return null;
}
}
/// <summary>
/// 将图片转换成byte[]
/// </summary>
/// <param name="img">图片</param>
/// <returns></returns>
public byte[] ImageToBytes(System.Drawing.Image img)
{
try
{
MemoryStream ms = new MemoryStream();
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
imagedata = ms.GetBuffer();
return imagedata;
}
catch
{
return null;
}
}
/// <summary>
/// 将byte[]转成图片
/// </summary>
/// <param name="by">byte[]数组</param>
/// <returns></returns>
public System.Drawing.Image BytesToImage(byte[] by)
{
try
{
MemoryStream ms = new MemoryStream(by);
return System.Drawing.Image.FromStream(ms);
}
catch
{
return null;
}
}
protected Bitmap BytesToBitmap(byte[] by)
{
try
{
MemoryStream ms = new MemoryStream(by);
Bitmap img = new Bitmap(ms);
return img;
}
catch
{
return null;
}
}
}
}
- Barcode条形码及对图片进行270翻转
- barcode制作条形码及破解
- BARCODE条形码插件及ean13
- barcode 条形码
- 通过Aspose.word.net 和 aspose.barcode.net替换指定位置图片生成条形码BarCode
- Excel 条形码生成(BarCode)
- java 处理 barCode(条形码)
- java 处理 barCode(条形码)
- java 处理 barCode(条形码)
- java 处理 barCode(条形码)
- iReport条形码Barcode
- java 处理 barCode(条形码)
- PHP生成条形码barcode
- java处理barCode条形码
- barcode.jar处理条形码
- Barcode生成条形码
- vue-barcode生成条形码
- 条形码控件使用(jquery-barcode)
- 使用共享中断
- 最小化当前应用程序
- jni获取mac地址的问题  -  棒槌网@Android开发论坛 - Powered by phpwind
- Container
- 源码网站推荐
- Barcode条形码及对图片进行270翻转
- 读文件操作
- 微软控件telerik report中怎样设置分页
- UVa 111 - History Grading
- log4j用法心得:
- Android-网络交互之客户端请求服务端资源-stoneson的专栏-  -  棒槌网@Android开发论坛 - Powered by phpwind
- Daniel Robbins sed 实例 POSIX 线程详解
- jvm terminated exit code =-1
- 数据挖掘所用到的一些数据集资源