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;
            }

        }

    }
}

原创粉丝点击