iT邦幫忙

0

C# winform怎麼在rdlc產生barcode?(產生barcode + 在rdlc呈現圖)

  • 分享至 

  • xImage
  •  

開發環境
winform
vs2013
C#

怎麼在rdlc產生barcode?
拆解來看的話,barcode其實就是一張圖.
問題就是如何產生barcode + 怎麼在rdlc呈現圖.

那怎麼產生barcode呢?
網路上有很多元件可以達到這個目的,這裡用的是Zen,你也可以用其它元件.

那rdlc產生圖有3種方法
1.External :設定url,就能直接呈現外部url的圖片,適用於動態圖片.
2.Embedded :直接上傳圖片,適用於靜態圖片,像是背景圖&標題圖片。
3.DataBase :從datatable拿資料,適用於動態圖片.
那我們要產生barcode,不是靜態的,沒法用Embedded.
然後希望將我們的值,在程式裡直接轉成Img,所以就用DataBase來呈現.

開始吧~~

🐤使用外掛元件產生barcode

install nuget
https://ithelp.ithome.com.tw/upload/images/20241001/20106764IqZGBW4Qtu.png

add form,add button,add picturebox
https://ithelp.ithome.com.tw/upload/images/20241001/2010676435IUT2gfUm.png

       private void button1_Click(object sender, EventArgs e)
        {
            string sCode = "12345678";
            var gImage = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum.Draw(sCode, 1000);
            pictureBox1.Image = gImage;
        }

跑跑看
https://ithelp.ithome.com.tw/upload/images/20241001/20106764pgtRIIy4O0.png

barcode產生出來,那要怎麼在報表上呈現呢?

🐤1.add dataset.xsd :建立datatable

add dataset
https://ithelp.ithome.com.tw/upload/images/20241001/20106764k1icMj4Fjg.png
add datatable
https://ithelp.ithome.com.tw/upload/images/20241001/20106764xPKa7WgDqT.png

建好datatable後,就能來建立報表了.

🐤2.編輯報表add rdlc :add dataset & add image

Add rdlc
https://ithelp.ithome.com.tw/upload/images/20241001/20106764Og5BjJIxmR.png

add datatset

https://ithelp.ithome.com.tw/upload/images/20241001/20106764Sm7TDb8pnY.png

choose dtatable
https://ithelp.ithome.com.tw/upload/images/20241001/201067646Tbv3y6rmR.png

加進來了,加進來後就能給image用了.
https://ithelp.ithome.com.tw/upload/images/20241001/20106764K0aigSZUr3.png

add image

https://ithelp.ithome.com.tw/upload/images/20241001/20106764TuZHrgt6v4.png
https://ithelp.ithome.com.tw/upload/images/20241001/20106764MOkzy9xRvc.png

🐤 3.form1 :add 前端reportViewer & 後端

前端
https://ithelp.ithome.com.tw/upload/images/20241001/20106764K6drcwSM0Z.png

後端

    private DataTable getDataTable()
    {
        string sCode = "12345678";
        var gImage = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum.Draw(sCode, 1000);
        pictureBox1.Image = gImage;

        // new資料表並add barcode img
        DataTable dt = new DataTable();
        dt.Columns.Add("bar", typeof(byte[]));

        using (MemoryStream ms = new MemoryStream())
        {
            gImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            dt.Rows.Add(ms.ToArray());
        } 
        return dt;
    }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = getDataTable();

            // setting
            reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
            reportViewer1.RefreshReport();
        }

就完成了
https://ithelp.ithome.com.tw/upload/images/20241001/20106764ORNvS0hGz1.png

🔥上面是將圖片轉成type[],如果要轉成string,也可以.

要在3個地方修改
1.xsd的tabletable
https://ithelp.ithome.com.tw/upload/images/20241001/20106764Gk6NAv4F1Z.png
2.cs的datatable
dt.Columns.Add("bar", typeof(string));
3.把type[]轉成base64
using (MemoryStream ms = new MemoryStream())
{
gImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
string ss = Convert.ToBase64String(ms.ToArray());
dt.Rows.Add(ss);
}

相關文章

圖的來源base64 https://ithelp.ithome.com.tw/articles/10284295


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言