C# 二維碼生成器

在二維碼中添加徽標可以增強品牌形象、美觀性和信任度。個性化、參與並建立信譽。帶徽標的定制 QR 碼可打造具有視覺吸引力、可識別且值得信賴的品牌體驗。在這篇博文中,我們將探討如何使用 QR 碼生成器 REST API 在 C# 中生成帶有自定義徽標的 QR 碼。我們將利用 GroupDocs.Signature Cloud SDK for .NET 來實現此功能。那麼,讓我們參與並學習如何使用 C# 生成帶徽標的二維碼

步驟大綱:

第 1 步:設置 C# 二維碼生成器 SDK

首先,請確保您的項目中安裝了適用於 .NET 的 GroupDocs.Signature Cloud SDK。您可以從 NuGet 包管理器 或在 .NET CLI 中使用以下命令將 GroupDocs.Signature Cloud SDK for .NET 安裝到您的項目:

dotnet add package GroupDocs.Signature-Cloud --version 23.4.0

第2步:初始化API客戶端

要初始化 API 客戶端,請從 儀表板 獲取您的 客戶端 ID客戶端密鑰 並添加如下所示的代碼:

//Get clientId & clientSecret from https://dashboard.groupdocs.cloud (free registration is required).
string clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string myStorage = "test-internal-storage";
var configuration = new Configuration(clientId, clientSecret);
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";

第三步:上傳文件

在生成帶徽標的二維碼之前,您需要上傳徽標以及要添加二維碼的文檔。使用以下任意一種方式將文檔和Logo上傳至雲存儲:

  • 使用儀表板
  • 使用瀏覽器中的上傳文件API一一上傳所有文件。
  • 使用下面給出的代碼示例以編程方式上傳:
// Create necessary API instances
var storageApi = new StorageApi(configuration);
var fileApi = new FileApi(configuration);
var path = @"H:\groupdocs-cloud-data";
var files = Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories);
foreach (var file in files)
{
var relativeFilePath = file.Replace(path, string.Empty).Trim(Path.DirectorySeparatorChar);
var response = storageApi.ObjectExists(new ObjectExistsRequest(relativeFilePath, myStorage));
if (response.Exists != null && !response.Exists.Value)
{
var fileStream = File.Open(file, FileMode.Open);
fileApi.UploadFile(new UploadFileRequest(relativeFilePath, fileStream, myStorage));
fileStream.Close();
}
}
view raw upload-file.cs hosted with ❤ by GitHub

因此,上傳的文件將在雲上儀表板的文件部分中可用。

步驟 4:使用 C# 生成帶有徽標的 QR 碼

以下步驟和示例代碼展示瞭如何使用 QR 碼生成器 REST API 在 C# 中生成帶有徽標的 QR 碼。

  1. 首先,使用您的 API 憑據創建一個配置對象。
  2. 接下來,實例化 SignApi 類以訪問簽名功能。
  3. 然後,設置簽名的二維碼選項,包括背景顏色、尺寸、對齊方式、位置和徽標文件路徑。
  4. 接下來,指定符號設置,包括文檔文件路徑和輸出文件路徑。
  5. 然後,使用指定的簽名設置創建簽名請求。
  6. 最後,使用 CreateSignatures 方法發出簽名請求並訪問響應以檢索有關簽名文檔的信息。

以下代碼示例演示如何使用 QR 碼創建器 REST API 在 C# 中創建帶有自定義徽標的 QR 碼。

var configuration = new GroupDocs.Signature.Cloud.Sdk.Client.Configuration("xxxxxx-xxxxxx-xxxxxxx-xxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxx");
var apiInstance = new GroupDocs.Signature.Cloud.Sdk.Api.SignApi(configuration);
//set QR Code options
var signQRCodeOptions = new GroupDocs.Signature.Cloud.Sdk.Model.SignQRCodeOptions()
{
BackgroundColor = new GroupDocs.Signature.Cloud.Sdk.Model.Color() { Web = "#fcfcfc" },
Width = 100,
Height = 80,
HorizontalAlignment = GroupDocs.Signature.Cloud.Sdk.Model.SignQRCodeOptions.HorizontalAlignmentEnum.Right,
VerticalAlignment = GroupDocs.Signature.Cloud.Sdk.Model.SignQRCodeOptions.VerticalAlignmentEnum.Center,
Left = 10,
Top = 100,
LocationMeasureType = GroupDocs.Signature.Cloud.Sdk.Model.SignQRCodeOptions.LocationMeasureTypeEnum.Pixels,
Margin = new GroupDocs.Signature.Cloud.Sdk.Model.Padding { Left = 10, Right = 10, Bottom = 10, Top = 10 },
MarginMeasureType = GroupDocs.Signature.Cloud.Sdk.Model.SignQRCodeOptions.MarginMeasureTypeEnum.Pixels,
LogoFilePath = "csharp-testing/logo.jpg"
};
// specify settings for creating request
var settings = new SignSettings();
settings.FileInfo = new GroupDocs.Signature.Cloud.Sdk.Model.FileInfo
{
FilePath = "csharp-testing/sample.pdf"
};
settings.Options.Add(signQRCodeOptions);
settings.SaveOptions.OutputFilePath = "csharp-testing/out_sample.pdf";
// create signature making request
var request = new GroupDocs.Signature.Cloud.Sdk.Model.Requests.CreateSignaturesRequest()
{
signSettings = settings
};
var response = apiInstance.CreateSignatures(request);
Debug.Print("FleName: " + response.FileInfo.FilePath);

第 5 步:下載結果文件

上一步中給出的代碼將生成的文件保存在雲端。要下載它,您可以使用以下代碼片段。

// initialize api
var fileApi = new FileApi(configuration);
// create download file request
var downloadRequest = new DownloadFileRequest("csharp-testing/merged-file.pdf", myStorage);
// download file
Stream downloadResponse = fileApi.DownloadFile(downloadRequest);
// save file in working directory
using (var fileStream = System.IO.File.Create("H:\\groupdocs-cloud-data\\merged-file.pdf"))
{
downloadResponse.Seek(0, SeekOrigin.Begin);
downloadResponse.CopyTo(fileStream);
}
Console.WriteLine("File downloaded successfully.");

常見問題解答:

我可以使用 JPG 以外的圖像文件格式作為徽標嗎?

是的,C# QR 代碼生成器 Rest API 支持各種圖像文件格式,例如徽標圖像的 PNG、JPEG、GIF 和 BMP。

我可以從 C# 中的字符串生成二維碼嗎?

是的,您可以看到 C# 從字符串生成二維碼 示例。

糾錯級別和二維碼版本是多少?

糾錯級別決定了QR碼中的冗餘量,影響其可讀性和糾錯能力。版本決定了二維碼的大小和數據容量。您可以根據您的要求選擇合適的值。

我可以自定義二維碼的外觀,例如更改顏色嗎?

是的,C# QR 代碼生成器 Rest API 提供了其他選項來自定義 QR 代碼的外觀,包括前景色、背景色和邊框顏色。

GroupDocs.Signature Cloud SDK for .NET 是付費服務嗎?

是的,GroupDocs.Signature Cloud 提供免費和付費計劃。您可以訪問網站以獲取有關定價和可用功能的更多信息。

結論

在這篇博文中,我們學習瞭如何使用 QR Code Generator Rest API 在 C# 中生成帶有徽標的 QR 碼。通過添加自定義徽標,您可以增強 QR 碼的視覺吸引力和品牌認知度。

適用於 .NET 的 GroupDocs.Signature Cloud SDK 簡化了流程並提供了各種自定義選項。請隨意探索文檔並嘗試不同的設置,以*在 C# 中生成符合您的品牌要求的 QR 代碼。

此外,您可以看到 API 參考 部分,它允許您直接通過瀏覽器可視化我們的 API 並與之交互。 C# SDK 的完整源代碼可以在 Github 上免費獲得。

最後,我們不斷撰寫有關不同文件格式的新博客文章,並使用 REST API 解析它們。因此,請聯繫我們以獲取最新更新。快樂編碼!

免費在線二維碼生成器

免費在線生成二維碼。請嘗試使用在線二維碼生成器應用程序來創建二維碼。這個二維碼創建應用程序是使用上述C#簽名庫開發的。

問一個問題

如果您對二維碼生成器有任何疑問或困惑,請隨時通過我們的論壇與我們聯繫。

也可以看看

以下是一些可能對您有幫助的相關文章: