Need to set smaller size image in button
Need to setup
picture
label
label
In same button
With my code text is over picture
Need to add some frame or something for picture
Thank you
My code
private void TopliNapitciButton_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
string conString = ConnectionClass.PullData(labelgodina.Text);
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("SELECT naziv+' - '+CAST(FORMAT(cijena_sa_porezom, 'N2') AS varchar), data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con);
var da = new SqlDataAdapter(cmd);
var ItemTable = new DataTable();
da.Fill(ItemTable);
con.Open();
Int32 count = ItemTable.Rows.Count;
con.Close();
int top = 190;
int left = 5;
for (int i = 1; i <= count; i++)
{
Button button = new Button();
button.Size = new Size(128, 128);
button.BackColor = Color.Transparent;
//button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.Font = new System.Drawing.Font("Trebuchet MS", 10);
button.TextAlign = ContentAlignment.BottomCenter;
button.BackgroundImageLayout = ImageLayout.Zoom;
button.Left = left;
button.Top = top;
button.Text = ItemTable.Rows[i - 1][0].ToString();
if (ItemTable.Rows[i - 1][1] != null)
{
try
{
byte[] _byte = (byte[])ItemTable.Rows[i - 1][1];
MemoryStream ms = new MemoryStream(_byte);
button.BackgroundImage = System.Drawing.Image.FromStream(ms);//bilo image
}
catch { }
}
button.Click += new EventHandler(this.btn_Click);
panel1.Controls.Add(button);
if (i % 4 == 0)
{
left = 5;
top += button.Height + 2;
}
else
{
left += button.Width + 2;
}
}
}
Tuhin PaulPosted Mar 28, 2024, 2:59 AM
Use a
TableLayoutPanelto organize the controls inside the button.TableLayoutPanelwithin each button to organize the image and labels.Buttonnow contains aTableLayoutPanel, which holds aPictureBoxfor the image and twoLabelcontrols for the name and price.Jayraj ChhayaPosted Mar 28, 2024, 6:45 AM
Hi,
To set a smaller size image in a button in C#, you can use the
BackgroundImageLayoutproperty of the button control. By setting theBackgroundImageLayoutproperty toImageLayout.Zoom, the image will be automatically resized to fit the button while maintaining its aspect ratio.Here's an example of how you can modify your code to set a smaller size image in the button:
By setting the
BackgroundImageLayoutproperty toImageLayout.Zoom, the image will be resized to fit the button while maintaining its aspect ratio. This ensures that the image is displayed in a smaller size without distorting its proportions.