T
T
taylor962019-12-17 22:44:26
C++ / C#
taylor96, 2019-12-17 22:44:26

How to display an image in a PictureBox on a form?

It is necessary to display in the PictureBox a picture with a chart built by the program in Excel.
In theory, the picture should be in the resources folder, but I don’t understand where it is saved and how to display it in the program itself in the PictureBox in this case?

using System;
using System.Windows.Forms;
using XL = Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XL.Application XL1 = new XL.Application(); // Объявляем объект
            XL1.Workbooks.Add(); // Добавляем рабочую книгу

            // Заполнить ячейки A2 (со 2 по 6)
            XL1.ActiveSheet.Cells[1, 2] = textBox1.Text;
            XL1.ActiveSheet.Cells[1, 3] = textBox2.Text;
            XL1.ActiveSheet.Cells[1, 4] = textBox3.Text;
            XL1.ActiveSheet.Cells[1, 5] = textBox4.Text;
            XL1.ActiveSheet.Cells[1, 6] = textBox5.Text;

            // Добавить новую диаграмму
            XL1.Charts.Add();

            // Задаем тип графика - диаграммма
            XL1.ActiveChart.ChartType = XL.XlChartType.xlPie;
            // Отключаем легенду и задаем имя диаграммы
            XL1.ActiveChart.HasLegend = false;
            XL1.ActiveChart.HasTitle = true;
            XL1.ActiveChart.ChartTitle.Characters.Text = "Diagram";

            // Сохраняем график в растровом файле
            XL1.ActiveChart.Export("myFile.jpg");
            //Bitmap myImage = new Bitmap("myFile.jpg");
            //pictureBox1.Image = (Image)myImage;
            XL1.Visible = true;
}

5df93016977f9350463023.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexV, 2019-12-18
@taylor96

And loading a picture into a PictureBox from a file is simple:
pictureBox1.Image = new Bitmap(@"C:\temp\DiskManagement.png");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question