.net 使用QR编码器和c#在中心显示徽标

fumotvh3  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(158)

我正在为我的工作构建一个应用程序,该应用程序使用QRCoder和C#(.net framework 4.7.2)创建一个QR Code。我如何将徽标放在中心位置?下面是控制器代码:我只需要把标志放在中间,星星.pnng就是标志。

QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(model.Url, 
         QRCodeGenerator.ECCLevel.Q);
            Bitmap bitmap = new Bitmap(@"~/images/star.png");
            Color clr = bitmap.GetPixel(0, 0);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(20);
            string imgFile = ConvertToFile(qrCodeImage, model.ImageType, model.Url);
            ViewBag.image = imgFile;
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AddHeader("Content-Disposition", "attachment; filename=" + NewFilename(imgFile) + ";");
            response.TransmitFile(imgFile);
            response.Flush();
            response.End();
            return View(BitmapToBytes(qrCodeImage));

以下是操作方法调用的其他方法:

private string GetFileName(string imgPath)
{
    string[] pathSplit = imgPath.Split('\\');
    return pathSplit[pathSplit.Length - 1];
}

private string GetTime()
{
    string time = DateTime.Now.ToString("HHmmssff");
    return time;
}

private static string ConvertToFile(Bitmap img, string ext, string url)
{
    SvgDocument doc;

    string filename = NameDownloadFile(url);
    //string pngFile = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\images\qrdemo.png";
    //string svgFile = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\images\output.svg";
    //string bmpFile = 
          @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\imagesqrdemosvg.bmp";
    string pngFile = $@"\\dmuweb1\e$\wwwroot\qrcodecreator\images\{filename}.png";
    string svgFile = $@"\\dmuweb1\e$\temp\{filename}.svg";
    string bmpFile = $@"\\dmuweb1\e$\temp\{filename}.bmp";
    //string dest = @"E:\temp\";
    string imgfile = "";
    if (ext == "PNG")
    {
        imgfile = pngFile;
        img.Save(pngFile, System.Drawing.Imaging.ImageFormat.Png);
    }
    else
    {
        imgfile = svgFile;
        if (System.IO.File.Exists(imgfile))
        {
            System.IO.File.Delete(imgfile);
        }
        //img.Save(bmpFile, System.Drawing.Imaging.ImageFormat.Bmp);
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, 
              QRCodeGenerator.ECCLevel.Q);
        SvgQRCode qrCode = new SvgQRCode(qrCodeData);
        string qrCodeAsSvg = qrCode.GetGraphic(20);
        string[] qrCodeAsSvgAr = { "cv" };
        qrCodeAsSvgAr[0] = qrCodeAsSvg;
        System.IO.File.WriteAllLines(imgfile, qrCodeAsSvgAr);
    }
    return imgfile;

}

private static string ConvertToFileOnBlur(Bitmap img, string ext)
{
    string pngFile = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\images\qrdemo.png";
    string svgFile = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\images\output.svg";
    string bmpFile = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\imagesqrdemosvg.bmp";
    string dest = @"C:\Projects\QRCodeCreatorApp\QRCodeCreator\images\";
    //string pngFile = @"\\dmuweb1\e$\wwwroot\qrcodecreator\images\qrdemo.png";
    //string svgFile = @"\\dmuweb1\e$\temp\output.svg";
    //string bmpFile = @"\\dmuweb1\e$\temp\qrdemosvg.bmp";
    string[] pngSplit = pngFile.Split('\\');
    string imgfile = "";
    if (ext == "PNG")
    {
        imgfile = pngFile;
        img.Save(pngFile, System.Drawing.Imaging.ImageFormat.Png);
    }
    else
    {
        imgfile = svgFile;
        img.Save(bmpFile, System.Drawing.Imaging.ImageFormat.Bmp);
        var converter = new GroupDocs.Conversion.Converter(bmpFile);
        var convertOptions = converter.GetPossibleConversions()["svg"].ConvertOptions;
        converter.Convert(svgFile, convertOptions);
    }

    return imgfile;
}
private static Byte[] BitmapToBytes(Bitmap img)
{
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();
    }
}
  • 谢谢-谢谢
liwlm1x9

liwlm1x91#

希望能有所帮助

QRCodeGenerator qrGenerator = new QRCodeGenerator();
                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(codeEntityId.ToString() + "|" + qrCodeEntity.AdditionalInfo, QRCodeGenerator.ECCLevel.Q);
                    QRCode qrCode = new QRCode(qrCodeData);
                    MemoryStream memoryStream = new MemoryStream();
                    Bitmap qrCodeImage;
                    if (imageLogo.Length != 0)
                    {
                        var logo = (Bitmap)Bitmap.FromStream(Operations.ByteArrayToStream(imageLogo));
                        var borderedLogo = CreateBorder(logo);
//here
                        qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, borderedLogo, 20, 1, true, Color.LightSlateGray);
                        qrCodeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                    {
                        qrCodeImage = qrCode.GetGraphic(20);
                        qrCodeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                    }

相关问题