0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でQRコード

Posted at

C#でQRコードを生成、表示させる方法

QRCoderを使う。

Nugetから、次の二つをインストールしておく。
+QRCoder
+System.Drawing.Common

MainWindows.xaml.cs
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qRCodeData = qrGenerator.CreateQrCode(textQrcode.Text, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qRCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(20);

            using (var ms = new System.IO.MemoryStream())
            {
                // MemoryStreamに書き出す
                qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

                ms.Seek(0, System.IO.SeekOrigin.Begin);

                Img.Source =
                    System.Windows.Media.Imaging.BitmapFrame.Create(
                        ms,
                        System.Windows.Media.Imaging.BitmapCreateOptions.None,
                        System.Windows.Media.Imaging.BitmapCacheOption.OnLoad
                    );
            }
        }
MainWindow.xaml
    <Grid>
        <TextBox
            x:Name="textQrcode"
            VerticalAlignment="Top" />
        <Button
            Name="btnCreate"
            Grid.Row="1"
            Click="btnCreate_Click"
            Content="生成"
            FontSize="12" />
        <Image
            x:Name="Img"
            Grid.Row="2"
            Margin="10"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
    </Grid>

ZXingは何度か試してみたが、無理だった。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?