Unable to access QRCode OR QRCodeGenerator.QRCode class while using QRCoder library version 1.3.5 - qr-code

I am unable to access QRCode class OR QRCodeGenerator.QRCode, while I am using QRCode.dll version 1.3.5 or I have checked version 1.3.3 but getting same issue.
public void GenerateQRCode(string code)
{
var imgUrl="";
//string code = txtCode.Text;
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
// I am getting error in above line, or I am using below line of code but getting error. Unable to access QRCode withQRCodeGenerator.
QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
imgUrl= "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
}
}
Above is the code which I am using in web API 2

Related

Xamarin: How to use existing database

I am trying to use an existing sqlite database in my app. Not sure how to do that.
I have seen a few suggestion online but didn't find anything helpful.
According to your description, you add one sqlite database in Assest in Android project, now you want to use this sqlite.
Firstly,install Sqlite.net.core in all three PCL, Android and IOS Projects, SQLite.Net.Platform.XamarinAndroid in Android Project.
Then you create interface IDatabase.cs in PCL:
public interface IDatabase
{
SQLite.Net.SQLiteConnection createconnection();
}
Implementing this interface in Android to get SqliteConnection.
public class GetDatabase : IDatabase
{
public SQLite.Net.SQLiteConnection createconnection()
{
var fileName = "SQLite.db3";
var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentPath, fileName);
if(!File.Exists(path))
{
using (BinaryReader br = new BinaryReader(Android.App.Application.Context.Assets.Open(fileName)))
{
using (BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int len = 0;
while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, len);
}
}
}
}
var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var conn = new SQLite.Net.SQLiteConnection(plat, path);
return conn;
//var plat = new SQLite.Net.Plataform.XamarinAndroid.SqlitePlatformAndroid();
}
Please note: SQLite.Net.Platform.XamarinAndroid earlier versions installation was successful. However, it will throw and error “Package SQLite.Net.Platform.XamarinAndroidN 3.1.1 is not compatible with monoandroid90 (MonoAndroid,Version=v9.0)” and package installation will fail. It’s a bug and solution is not available of today.You can copy this dll from this link to add your android project.
More detailed info, please take a look:
Use a local database in Xamarin

File Uploading using HttpClient in .Net core 3.0 Console Application

I am trying to upload file using HttpClient in Asp.net Core 3, but It is not uploading file to the server. If I try to upload file to the server via Postman, it works.
Below is my simple code to upload file:
HttpClient _client = new HttpClient();
var stream = new FileStream("main.txt", FileMode.Open);
byte[] fileBytes = new byte[stream.Length];
stream.Write(fileBytes, 0, (int)stream.Length);
stream.Dispose();
using (var content = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Test",
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
content.Add(fileContent);
_client.PostAsync("http://192.168.56.1:8000", content);
}
As I said above it is working with Postman. I am putting a screenshot which shows that how I doing with Postman.
when I debug the code, so I get below error.
One solution is that you could use MemoryStream to transform the content of the file. Your method will cause the content in the main.txt file to become empty.
Change your code like this:
HttpClient _client = new HttpClient();
Stream stream = new FileStream("main.txt", FileMode.Open);​
MemoryStream ms = new MemoryStream();​
stream.CopyTo(ms);​
byte[] fileBytes = ms.ToArray();​
ms.Dispose(); ​
Another way is that use System.IO.File.ReadAllBytes(filePath).
Try to post file using below example code instead, refer to my answer.
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
//replace with your own file path, below use an txt in wwwroot for example
string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "main.txt");
byte[] file = System.IO.File.ReadAllBytes(filePath);
var byteArrayContent = new ByteArrayContent(file);
content.Add(byteArrayContent, "file", "main.txt");
var url = "https://localhost:5001/foo/bar";
var result = await client.PostAsync(url, content);
}
}
foo/bar action
[HttpPost]
[Route("foo/bar")]
public IActionResult ProcessData([FromForm]IFormFile file)
{
//your logic to upload file
}
I've downloaded the server and tested it with this code. The server returns 200 OK
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
using (var fileStream = new FileStream("test.txt", FileMode.Open))
{
var fileContent = new StreamContent(fileStream);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
content.Add(fileContent, "file", "test.txt");
var response = await client.PostAsync("http://192.168.56.1:8000/", content);
}
}
}

Streaming a blob from Azure storage - Cannot access a closed Stream

I am using asp.net webforms.
I have pdfs in Azure storage that I need to process. I am using PDFJet library in order to do that.
I would like to
stream the pdf without downloading it as I have to process a large number of pdfs.
I am using the following function to stream the pdfs from Azure:
public MemoryStream DownloadToMemoryStream(DTO.BlobUpload b)
{
CloudStorageAccount storageAccount = Conn.SNString(b.OrgID);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
CloudBlockBlob blob = container.GetBlockBlobReference(b.FileName);
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
return ms;
}
}
And in the aspx.cs page the following code to read the pdf stream:
BufferedStream pdfScript = new BufferedStream(new FileStream(ScriptPath + Script, FileMode.Open));
SortedDictionary<Int32, PDFobj> objects = pdfFinalScript.Read(pdfScript);
However I get the error message: Cannot access a closed Stream
If I download the pdf to disk, this is the function I use, this work but it is not practical:
blockBlob.DownloadToFile(b.LocalPath + b.FileName, FileMode.Create);
BufferedStream pdfScript = new BufferedStream(new FileStream(ScriptPath + Script, FileMode.Open));
Thank you for your help.
Cannot access a closed Stream
According to error information, it indicates that you need to reset stream position.
Please have a try to reset the stream position before return it.
blob.DownloadToStream(ms);
ms.Position = 0; //add this code
return ms;
Updated:
ms was closed if out of using section. So please have a try to use the following code.
MemoryStream stream = new MemoryStream();
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
ms.Position = 0
ms.CopyTo(stream);
stream.Position = 0;
return stream;
}

Tridion core service How to download binary file of a multimedia component

I have a requirement where I need to download binary file of a multimedia component but when I access the properties exposed of BinaryContentData class then there is no property to download an image file. Although for uploading file, Core Service have a property namely UploadFromFile.
So is there a way to download binary file to a temp location. Below is the code I am using:
core_service.ServiceReference1.SessionAwareCoreService2010Client client = new SessionAwareCoreService2010Client();
client.ClientCredentials.Windows.ClientCredential.UserName = "myUserName";
client.ClientCredentials.Windows.ClientCredential.Password = "myPassword"; client.Open();
ComponentData component = (ComponentData)client.TryCheckOut(
multimediaComponentURI, new ReadOptions());
BinaryContentData binaryData = component.BinaryContent;
Please Suggest.
There is a helper function called streamDownloadClient.DownloadBinaryContent inside Tridion.ContentManager.CoreService.Client.dll that you can use.
I have created the following function that I usually reuse for that purpose:
private static void CreateBinaryFromMultimediaComponent(string tcm)
{
Tridion.ContentManager.CoreService.Client.StreamDownloadClient streamDownloadClient = new StreamDownloadClient();
SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2011");
ComponentData multimediaComponent = client.Read(tcm, new ReadOptions()) as ComponentData;
// Generate you own file name, and file location
string file = "D:\\MyTempLocation\\" + Path.GetFilename(multimediaComponent.BinaryContent.Filename);;
// Write out the existing file from Tridion
FileStream fs = File.Create(file);
byte[] binaryContent = null;
if (multimediaComponent.BinaryContent.FileSize != -1)
{
Stream tempStream = streamDownloadClient.DownloadBinaryContent(tcm);
var memoryStream = new MemoryStream();
tempStream.CopyTo(memoryStream);
binaryContent = memoryStream.ToArray();
}
fs.Write(binaryContent, 0, binaryContent.Length);
fs.Close();
}

How to get pdf file bookmarks using c#

How to get bookmarks from the pdf file using c# .
Which is the best library that do this .
i found this code to get all bookmarks from a pdf file using iTextSharp:
public void ExportBookmarksToXml(string SourcePdfPath, string xmlOutputPath, string Password = "")
{
PdfReader reader = new PdfReader(SourcePdfPath, new System.Text.ASCIIEncoding().GetBytes(Password));
//Collection of bookmarks
IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
using (MemoryStream memoryStream = new MemoryStream())
{
SimpleBookmark.ExportToXML(bookmarks, memoryStream, "ISO8859-1", true);
//MessageBox.Show(bookmarks[0].Values.ToString());
File.WriteAllBytes(xmlOutputPath, memoryStream.ToArray());
}
}

Resources