asp.netc#中二维码图片无法大小调整
创始人
2024-09-17 07:00:39
0

问题可能是由于生成二维码的代码没有设置图片的大小导致的。解决方法是通过设置二维码图片的大小属性来调整图片大小。

以下代码示例演示如何生成指定大小的二维码图片:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Web;
using ZXing;
using ZXing.Common;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string text = "hello world!";//生成二维码的文本内容
            int width = 300;//图片宽度
            int height = 300;//图片高度
            var barcodeWriter = new BarcodeWriterPixelData
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width = width,
                    Margin = 1,
                    CharacterSet = "UTF-8"
                }
            };

            var pixelData = barcodeWriter.Write(text);
            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            using (var ms = new MemoryStream())
            {
                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                try
                {
                    // Copy the source pixels into the destination bitmap
                    System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }

                Response.ContentType = "image/png";
                bitmap.Save(Response.OutputStream, ImageFormat.Png);
            }
        }
    }
}

在上述代码示例中,通过设置heightwidth属性来指定二维码图片的大小,从而生成指定大小的二维码图片。

相关内容

热门资讯

外挂绝活!uupoker透视,... 外挂绝活!uupoker透视,扑克之星辅助-总是是真的有辅助教程(哔哩哔哩)1、点击下载安装,扑克之...
外挂妙计!智星菠萝可以辅助吗,... 外挂妙计!智星菠萝可以辅助吗,佛手在线大菠萝为什么都输-一贯真的是有辅助方法(哔哩哔哩)1、下载好佛...
外挂攻略!来玩app 德州 辅... 外挂攻略!来玩app 德州 辅助,约局吧如何查看是否有挂-果然一直总是有辅助方法(哔哩哔哩)1、每一...
外挂烘培!sohoo辅助,po... 外挂烘培!sohoo辅助,pokeplus脚本-真是有辅助教程(哔哩哔哩)一、pokeplus脚本可...
外挂大纲!大菠萝免费辅助,哈糖... 外挂大纲!大菠萝免费辅助,哈糖大菠萝软件下载-切实存在有辅助工具(哔哩哔哩)1、哈糖大菠萝软件下载透...
外挂教程书!聚星ai辅助工具激... 外挂教程书!聚星ai辅助工具激活码,德普之星透视辅助软件激活码-原来有辅助技巧(哔哩哔哩)1、很好的...
外挂资料!佛手大菠萝有挂吗,拱... 外挂资料!佛手大菠萝有挂吗,拱趴大菠萝万能挂-竟然一直总是有辅助脚本(哔哩哔哩)1、超多福利:超高返...
外挂攻略!aa poker辅助... 外挂攻略!aa poker辅助,德州局透视-原来是真的有辅助教程(哔哩哔哩)1、许多玩家不知道德州局...
外挂手筋!德州局脚本,poke... 外挂手筋!德州局脚本,pokemomo辅助工具-原来是有辅助脚本(哔哩哔哩)1、玩家可以在pokem...
外挂办法!pokermaste... 外挂办法!pokermaster辅助器,pokerworld修改器-好像是真的有辅助方法(哔哩哔哩)...