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属性来指定二维码图片的大小,从而生成指定大小的二维码图片。

相关内容

热门资讯

第5新版(来玩app)外挂透明... 此外,数据分析德州(来玩app)辅助神器app还具备辅助透视行为开挂功能,通过对客户来玩app透明挂...
8个打法!wepoke透明挂辅... 8个打法!wepoke透明挂辅助软件是真的(插件)透视(存在有挂)-哔哩哔哩;玩家必备必赢加哟《13...
四小程序!雀神微信小程序怎么开... 您好,雀神微信小程序怎么开挂这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多...
1个大厅!德扑ai操作(手牌胜... 1个大厅!德扑ai操作(手牌胜率计算工具)辅助透视(有挂实锤)-哔哩哔哩1、每一步都需要思考,不同水...
九黑科技(wepoke app... 九黑科技(wepoke app)外挂透明挂辅助APP(透视)技巧教程(真是有挂)-哔哩哔哩;wepo...
5个app技巧!微扑克机制技巧... 1、5个app技巧!微扑克机制技巧(插件)辅助透视(新版有挂)-哔哩哔哩(UU poker、微扑克机...
第五真的有挂!雀神微信小程序免... 第五真的有挂!雀神微信小程序免费辅助器(辅助挂)太坑了原来真的有挂(有挂透明挂)-哔哩哔哩1、用户打...
第一个脚本!德扑之星软件模拟器... 第一个脚本!德扑之星软件模拟器(作弊)透视辅助(揭秘有挂)-哔哩哔哩;支持多人共享记分板与复盘,通过...
四个规律!wepoke科技(软... 四个规律!wepoke科技(软件透明挂)辅助挂(有挂猫腻)-哔哩哔哩;wepoke科技黑科技是一款具...
第三开挂(aapoker开发)... 第三开挂(aapoker开发)外挂透明挂辅助机制(透视)高科技教程(有挂讲解)-哔哩哔哩是一款可以让...