C# zxing二維碼寫入的實(shí)例代碼
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("請(qǐng)輸入需要轉(zhuǎn)換的信息!");
return;
}
string content = textBox1.Text;
Hashtable hints= new Hashtable();
hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//糾錯(cuò)級(jí)別
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//編碼格式
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "*.png|*.png";
sFD.AddExtension = true;
try
{
if (sFD.ShowDialog() == DialogResult.OK)
{
writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
System.Drawing.Imaging.EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters();
eps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
Bitmap bmap = toBitmap(matrix);
bmap.Save(file, format);
}
public static Bitmap toBitmap(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("Purple") : ColorTranslator.FromHtml("0xFFFFFFFF"));//可以自定義顏色和背景色
}
}
return bmap;
}
相關(guān)文章
C# 泛型數(shù)組學(xué)習(xí)小結(jié)
C# 泛型數(shù)組學(xué)習(xí)中我們需要注意什么事項(xiàng)呢?C# 泛型數(shù)組的使用又是如何呢?那么本文就向你詳細(xì)介紹這方面的內(nèi)容2012-09-09WPF開發(fā)之實(shí)現(xiàn)一種三軸機(jī)械手控件
這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)簡(jiǎn)單一種三軸機(jī)械手控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-01-01關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.
本篇文章,小編為大家介紹關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介紹方法,有需要的朋友可以參考一下2013-04-04C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法
這篇文章主要介紹了C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11