二维码及打印

1.条码打印

C# 调用 BarTender 程序,需要下载

Interop.BarTender.dll

1.System.Drawing.Image和System.Drawing.Bitmap这两个类有什么联系和区别?

System.Drawing.Bitmap 是位图 对应的是矢量图

System.Drawing.Bitmap : System.Drawing.Image
{ }

https://www.cnblogs.com/nele/p/5039092.html

2.修改网络上的图片大小

/// <summary>
        /// 图片大小裁剪
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public byte[] ResizeImage(string filePath)
        {

            WebRequest request = (WebRequest)HttpWebRequest.Create(filePath);
            WebResponse response = request.GetResponse();   // 这两句表示从网络上获取图片,并且转成文件流,文件流是非托管的using (Stream stream = response.GetResponseStream())
            {
                Bitmap bm = (Bitmap)Image.FromStream(stream);

                bm = GetThumbnail(bm, 50, 40);
                MemoryStream ms = new MemoryStream(); // 用这个通常是为了将图片转二进制,或者二进制转为流
                bm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以,至于区别么,下面有解释
                ms.Close();
                return bytes;
            }

        }

 

/// <summary>
        /// 修改图片的大小
        /// </summary>
        /// <param name="b"></param>
        /// <param name="destHeight"></param>
        /// <param name="destWidth"></param>
        /// <returns></returns>
        public Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
        {
            System.Drawing.Image imgSource = b;
            System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
            int sW = 0, sH = 0;
            // 按比例缩放           
            int sWidth = imgSource.Width;
            int sHeight = imgSource.Height;
            if (sHeight > destHeight || sWidth > destWidth)
            {
                if ((sWidth * destHeight) > (sHeight * destWidth))
                {
                    sW = destWidth;
                    sH = (destWidth * sHeight) / sWidth;
                }
                else
                {
                    sH = destHeight;
                    sW = (sWidth * destHeight) / sHeight;
                }
            }
            else
            {
// 如果原来的长和宽都小于设置的 sW
= sWidth; sH = sHeight; } Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Transparent); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; imgSource.Dispose(); return outBmp; }

 

/// <summary>
        /// 得到图像
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public FileResult GetAvator(string path)
        {
            return File(service.ResizeImage(path), "image/png");
        }

3. 生成二维码

网上有 QRCoder 和 ZXing.Net(过时) , ThoughtWorks.QRCode (二维码,大小只有三种) 三种方法, 推荐 QRCoder ,需要GuNet 安装

图片是 Bitmap

 4.打印

1.web 网页可以使用 

posted @ 2022-04-22 09:44  海龟123  阅读(279)  评论(0编辑  收藏  举报