【问题标题】:Retrive an image from a field database path to a datagrid从字段数据库路径检索图像到数据网格
【发布时间】:2018-02-01 10:40:39
【问题描述】:

我需要将徽标团队放在团队名称旁边,我有一个包含团队名称的表和一个名为徽标的字段,如何直接从存储在文件夹图像中的路径中检索存储在文件夹图像中的徽标表中的字段, 我分享我的桌子的图像,以便你们专家给我一个如何实现的方向! 问候 enter image description here

【问题讨论】:

  • 我需要将徽标放入数据网格中!
  • 图像数据网格中的列类型是什么?
  • 它是一个** varchar!**
  • 我需要 datagrid 使用正确的语法读取路径来检索图像!
  • Datagrid 不会根据路径自动渲染图像。相反,您需要根据数据库中的路径绑定图像。

标签: asp.net visual-studio datagrid


【解决方案1】:

在 asp.net 的数据网格中显示图像非常简单。

 <asp:TemplateField  HeaderText="IMAGE"  ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Image ID="img" ImageUrl='<%#(Eval("IMAGE"))%>'  runat="server" Width="40px" Height="50px" CssClass="pic zoom" />
                    </ItemTemplate>

                </asp:TemplateField>

现在允许您的数据表有一个图像列,您可以在其中保存徽标图像的路径这是我的数据表。

它是输出图像

【讨论】:

    【解决方案2】:

    这是我的示例代码。 dataGridView1 是一个 DataGridView 控件。

    public partial class Form1 : Form
    {
        public ObservableCollection<Person> Persons { get; set; }
        public Form1()
        {
            InitializeComponent();
            var Persons = new ObservableCollection<Person>();
    
            Persons.Add(new Person(1, "John", 5));
            Persons.Add(new Person(2, "Ed", 4));
            Persons.Add(new Person(3, "Sara", 3));
            dataGridView1.DataSource = Persons;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    }
    
    public class Person
    {
        public int Rank { get; set; }
        public string Name { get; private set; }
        public int Score { get; set; }
        public Image Star { get; set; }
    
        public Person(int rank, string name, int score)
        {
            Rank = rank;
            Name = name;
            Score = score;
            Star = DrawStarImage(score);
        }
        private Image DrawStarImage(int starCount)
        {
            var starIcon = Image.FromFile("E:\\temp\\download.jpg");
    
            // the image of all stars (the width has to be the width of one star multiplied by the count of stars)
            var image = new Bitmap(starCount * starIcon.Width, starIcon.Height);
    
            using (var g = Graphics.FromImage(image))
            {
                for (int i = 0; i < starCount; i++)
                {
                    // place the star icon to its position in the loop
                    int x = (i * starIcon.Width);
    
                    // https://msdn.microsoft.com/de-de/library/dbsak4dc(v=vs.110).aspx
                    g.DrawImage(starIcon, x, 0, starIcon.Width, starIcon.Height);
                }
    
                g.Flush();
            }
            return image;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多