wpf时间显示代码

I've half-jokingly said that there's never a good reason to use a QR Code. However, I'm working on an MVP (minimally viable product) for a small startup with Greg Shackles and we actually have a good reason to use one. We have a mobile device, a Web Site, and a Windows Application, and a QR Code is a pretty quick way to move data between the mobile device and the other applications without the mess of Bluetooth pairing.

我开玩笑地说,从来没有充分的理由使用QR码。 但是,我正在与Greg Shackles一起为一家小型初创公司开发MVP(最低可行产品),实际上我们有充分的理由使用它。 我们有一个移动设备,一个网站和一个Windows应用程序,QR码是一种非常快速的方法,可以在移动设备和其他应用程序之间移动数据,而不会造成蓝牙配对的麻烦。

As I mentioned, we display the QR code on an ASP.NET website, as well as within a Windows app that happens to be written in WPF. The iPhone app uses C# and Xamarin.

正如我提到的,我们在ASP.NET网站上以及恰好用WPF编写的Windows应用程序中显示QR代码。 iPhone应用程序使用C#和Xamarin。

There's a great QR Code library called "ZXing" (Zebra Crossing) with ports in Java and also in C#. The C#/.NET one, ZXing.NET is a really fantastically well put together project with assemblies available for everything from .NET 2 to 4.5, Windows RT, Unity3D, Portable libraries and more. The site is filled with demo clients as well, although we didn't find one for ASP.NET or WPF. No matter, it's all just generating and showing PNGs.

有一个很棒的QR代码库,名为“ ZXing”(斑马线),带有Java和C#中的端口。 ZXing.NET是C#/。NET的一个非常好组合的项目,具有可用于.NET 2到4.5,Windows RT,Unity3D,可移植库等的程序集。 尽管我们找不到用于ASP.NET或WPF的示例客户端,但该站点也充满了演示客户端。 没关系,所有这些都只是生成和显示PNG。

I pulled in ZXing.NET from the NuGet package here, just install-package ZXing.Net.

我从此处的NuGet包中拉入ZXing.NET ,只是安装了ZXing.Net包

如何在ASP.NET中显示QR码 (How to display a QR code in ASP.NET)

If you're generating a QR code with ASP.NET MVC, you'll have the page that the code lives on, but then you'll need to decide if you want to make an HTTP Handler that generates the graphic, like:

如果要使用ASP.NET MVC生成QR代码,则将具有该代码所在的页面,但随后需要确定是否要制作可生成图形的HTTP处理程序,例如:

<img src="/path/to/httphandlerthatmakesQRcodepng">

or, you could take a different approach like we did, and embed the code in the HTML page itself.

或者,您可以像我们一样采用其他方法,然后将代码嵌入HTML页面本身。

Greg used an HTML Helper to output the entire image tag, including the inlined image, as in:

Greg使用HTML Helper输出整个图像标签,包括内联图像,如下所示:

<img src="https://img-blog.csdnimg.cn/2022010618441177439.gif" />            

Images in HTML directly as Data URIs are super fun and I think, often forgotten. If you show one to the average web dev they'll say "oh, ya...I knew about those, but never really used it." In fact, Data URIs have been around for a LONG time. Learn more about them at DataUrl.net.

直接将HTML中的图像作为数据URI很好玩,我认为这经常被人们遗忘。 如果您向一般的Web开发人员展示一个,他们会说:“哦,是的,我知道这些,但从未真正使用过。” 实际上,数据URI已经存在了很长时间。 在DataUrl.net上了解有关它们的更多信息。

Here's generating a QR Code within ASP.NET MVC from an HTML Helper:

这是通过HTML Helper在ASP.NET MVC中生成QR代码:

public static class HtmlHelperExtensions{    public static IHtmlString GenerateRelayQrCode(this HtmlHelper html, string groupName, int height = 250, int width = 250, int margin = 0)    {        var qrValue = "whatever data you want to put in here";        var barcodeWriter = new BarcodeWriter        {            Format = BarcodeFormat.QR_CODE,            Options = new EncodingOptions            {                Height = height,                Width = width,                Margin = margin            }        };

        using (var bitmap = barcodeWriter.Write(qrValue))        using (var stream = new MemoryStream())        {            bitmap.Save(stream, ImageFormat.Gif);

            var img = new TagBuilder("img");            img.MergeAttribute("alt", "your alt tag");            img.Attributes.Add("src", String.Format("data:image/gif;base64,{0}",                 Convert.ToBase64String(stream.ToArray())));

            return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));        }    }}

Nice and simple. The BarcodeWriter class within ZXing.NET does the hard work. We don't need to save our QR Code to disk, and because we're doing it inline from our HTML page via this helper, there's no need for a separate call to get the image. Also, the caching policy that we decide to use for the page applies to the image within, simplifying things vs. two calls.

漂亮又简单。 ZXing.NET中的BarcodeWriter类很辛苦。 我们不需要将QR代码保存到磁盘上,并且由于我们是通过此帮助程序从HTML页面内嵌地完成的,因此无需单独调用即可获取图像。 同样,我们决定用于页面的缓存策略也适用于其中的图像,从而简化了两次调用。

如何在WPF中显示QR码 (How to display a QR code in WPF)

Note: This code here may be wrong. I'm happy to hear your suggestion, Dear Reader, because I'm either missing something completely or there is no clear and clean way to get from a System.Drawing.Bitmap to a System.Windows.Media.imaging.BitmapImage. The little dance here with the saving to a MemoryStream, then moving into a BitmapImage (with the unintuitive but totally required setting of CacheOption as well) just sets off my Spideysense. It can't be right, although it works.

注意:这里的代码可能是错误的。 亲爱的读者,我很高兴听到您的建议,因为我要么完全丢失了某些东西,要么没有从System.Drawing.Bitmap到System.Windows.Media.imaging.BitmapImage的清晰明了的方法。 保存到MemoryStream,然后移动到BitmapImage(还有不直观但又完全需要的CacheOption设置)的情况下,这引起了我的Spideysense的兴趣。 尽管可以,但这不是正确的。

I'll update the post when/if a cleaner way is found.

如果发现更干净的方式,我将更新帖子。

See below for update!

请参阅下面的更新!

First, the exact same BarcodeWriter usage from the ZXing.NET library.

首先,来自ZXing.NET库的BarcodeWriter用法完全相同。

var qrcode = new QRCodeWriter();var qrValue = "your magic here";

var barcodeWriter = new BarcodeWriter{    Format = BarcodeFormat.QR_CODE,    Options = new EncodingOptions    {        Height = 300,        Width = 300,        Margin = 1    }};

using (var bitmap = barcodeWriter.Write(qrValue))using (var stream = new MemoryStream()){    bitmap.Save(stream, ImageFormat.Png);

    BitmapImage bi = new BitmapImage();      bi.BeginInit();        stream.Seek(0, SeekOrigin.Begin);        bi.StreamSource = stream;        bi.CacheOption = BitmapCacheOption.OnLoad;      bi.EndInit();      QRCode.Source = bi; //A WPF Image control}

Later, writing the Bitmap to a MemoryStream for manipulation, except in this case, we're putting the QR Code into the Source property of a WPF Image Control.

稍后,将位图写入MemoryStream进行操作,除非在这种情况下,我们将QR代码放入WPF图像控件的Source属性中。

UPDATE: Thomas Levesque in the comments below suggests an extension within System.Windows.Interop (which explains me not finding it) called CreateBitmapSourceFromHBitmap. This still feels gross as it appears to requires a call to the native DeleteObject, but regardless, that's the price you pay I guess. It looks like this:

更新:以下评论中的Thomas Levesque建议在System.Windows.Interop中进行扩展(这说明我找不到它),该扩展名为CreateBitmapSourceFromHBitmap 。 这似乎仍然很麻烦,因为它似乎需要调用本地DeleteObject,但是不管怎么说,这就是您要付出的代价。 看起来像这样:

using (var bitmap = barcodeWriter.Write(qrValue)){    var hbmp = bitmap.GetHbitmap();    try    {        var source = Imaging.CreateBitmapSourceFromHBitmap(hbmp, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());        QRCode.Source = source;    }    finally    {        DeleteObject(hbmp);    }}

It works well!

效果很好!

翻译自: https://www.hanselman.com/blog/how-to-display-a-qr-code-in-aspnet-and-wpf

wpf时间显示代码

wpf时间显示代码_如何在ASP.NET和WPF中显示QR代码相关推荐

  1. python图片显示中文_解决Python pandas plot输出图形中显示中文乱码问题

    解决方式一: import matplotlib #1. 获取matplotlibrc文件所在路径 matplotlib.matplotlib_fname() #Out[3]: u'd:\\Anaco ...

  2. asp 判断数组等于_如何在 ASP.NET Core MVC 中处理 404 错误

    译文链接:https://www.infoworld.com/article/3545304/how-to-handle-404-errors-in-aspnet-core-mvc.html http ...

  3. python输入逗号分隔值文件_如何在Python(Pygame)中显示逗号分隔值(.txt)文件中的前10个高分...

    我正在为我的游戏创建一个高分表.它以下列格式将值附加到.txt文件: 5.234,0,0,5234 6.345,1,1,8345 1.649,0,1,2649 2.25,0,1,3250 ...等等 ...

  4. 获取异常信息_如何在 ASP.NET Core 中实现全局异常拦截

    异常是一种运行时错误,当异常没有得到适当的处理,很可能会导致你的程序意外终止,这篇就来讨论一下如何在 ASP.Net Core MVC 中实现全局异常处理,我会用一些 样例代码 和 截图 来说明这些概 ...

  5. chart.js ajax 折线图,如何在ChartJs 的折线图中显示数据值或索引标签

    如何在Chartjs的折线图中显示数据值或索引值,如图所示: 图例.PNG 简介 Chart.js是一款不依赖任何外部js库的图标插件,具体的使用方法可查看Chart.js官网. 需求 Chart.j ...

  6. 要显示的8个字符已存放在以BUF开始的存储区单元中(称为显示缓冲区),依次送到LED显示器中显示。CPU通过P0口和P2口控制8位LED显示器,LED为共阴极显示器。

    目录 题目 1.0绘制电路图protues: 2.0程序代码: 3.0 protues仿真: 在uvision下生成hex文件 protues添加hex文件 仿真 题目 要显示的8个字符已存放在以BU ...

  7. wordpress代码_如何在WordPress网站上轻松显示代码

    wordpress代码 Do you want to display code in your WordPress blog posts? If you tried to add code like ...

  8. asp.net应用程序_如何在ASP.NET中为聊天应用程序构建键入指示器

    asp.net应用程序 by Neo Ighodaro 由新Ighodaro 如何在ASP.NET中为聊天应用程序构建键入指示器 (How to build a typing indicator fo ...

  9. asp绑定gridview属性_如何在ASP.NET Core中自定义Azure Storage File Provider

    主题:如何在ASP.NET Core中自定义Azure Storage File Provider 作者: Lamond Lu 地址:  https://www.cnblogs.com/lwqlun/ ...

最新文章

  1. python分数运算使用Fraction模块
  2. python中递归函数的实例_Python 递归函数详解及实例
  3. 如何在Keras中训练大型数据集
  4. ASSERT()是干什么用的
  5. 多线程随机数组生成+双线程快速排序(C++实现)(0.2秒排100W个数字)
  6. linux中文件的编辑 写入 读取 光标的位置 以及相应的补充
  7. URAL1519 Formula 1 —— 插头DP
  8. python安装常见问题_Python常见问题
  9. STM32工作笔记0052---串口通信原理--UART
  10. 二叉树的遍历实验报告C语言,二叉树的建立与遍历实验报告(c语言编写,附源代码)...
  11. 【window操作系统下Github版本的回滚问题】
  12. 计算机设备运输规范,《电子计算机机房设计规范》GB50174-93
  13. 西门子step7安装注册表删除_西门子STEP7程序安装与卸载教程
  14. PLC Outstudio 使用教程
  15. AMiner发布2022 AI 2000人工智能最具影响力学者名单
  16. 我国学生被美深泉学院录取 每周20小时放牛种草
  17. Eclipse开发工具--使用JDT开发java程序
  18. 低信噪比MIMO SC-FDE系统中信道估计的研究与实现
  19. 黑盒测试技术(Decision Tables 决策表法,又称判定表法)——软件质量保证与测试
  20. python报错:fails to pass a sanity check due to a bug in the windows runtime

热门文章

  1. 股价上扬,战略*组织力正成为汽车之家的“新增长门票”
  2. java 4-5随机数_例4-5 猜数游戏 产生随机数(示例代码)
  3. R绘图 | 堆叠柱状图
  4. AutoCAD快捷键
  5. Lingoes 简明西汉/汉西词典
  6. MySQL性能调优与架构设计(二)—— MySQL存储引擎简介
  7. CC2530入门篇————实现四盏灯全亮
  8. 数据治理的概念、难点和最佳实践方法
  9. C# Media Player控件
  10. 暴风集团实际控制人冯鑫因涉嫌犯罪被公安机关采取强制措施