首页 > 移动平台 > 详细

C# access the embeded resource ,start windows form in console application

时间:2020-06-17 19:49:43      阅读:73      评论:0      收藏:0      [点我收藏+]

1.Add new folder  Resources in project;

2.Add resource such as picture in the Resource folder;

3.Set the picture‘s Build Action as  Embedded Resource in picture‘s properties.

4.Build.

5.Access the embeded resource fill via assembly and GetManifestResourceStream

Attention the aforementioned picture‘s file name must include namespace.folder.filename.ext;such as "ConsoleApp30.Resources.LYF4.jpg"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using System.Drawing; 

namespace ConsoleApp30
{
    class Program
    {
        static System.Windows.Forms.Form fm;
        [STAThread]
        static void Main(string[] args)
        {
            AccessEmbededResources();
            Application.EnableVisualStyles();
            Application.Run(fm);            
            Console.ReadLine();
        }

        static void AccessEmbededResources()
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string fileName = "ConsoleApp30.Resources.LYF4.jpg";           
            using (Stream sm = asm.GetManifestResourceStream(fileName))
            {
                Image img = System.Drawing.Image.FromStream(sm);
                fm = new Form();
                fm.WindowState = FormWindowState.Maximized;
                fm.FormBorderStyle = FormBorderStyle.Fixed3D;
                fm.BackgroundImage = img;
                fm.BackgroundImageLayout = ImageLayout.Zoom;
                fm.TopMost = true;               
                fm.Show();
            }
        }
    }
}

 

C# access the embeded resource ,start windows form in console application

原文:https://www.cnblogs.com/Fred1987/p/13153997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!