在我的應用程序中,我想創建一個位圖來繪製,然後在多個窗體上顯示它。我需要集中協調位圖的更新,所以我想根據計時器從MAIN那裏做到這一點。位圖將在創建時重新調整到屏幕的全尺寸大小。這是否是這種造成異常的調整嘗試?C#:是否有可能從program.cs中的資源圖像創建一個位圖?
的代碼看起來不錯,和VS不抱怨,直到我試圖調試,然後拋出一個「參數是不是在其中創建位圖線有效」例外。
我有選擇性地壓縮我下面的代碼:
using System;
using System.Windows.Forms;
using System.IO;
using System.Timers;
using System.Drawing;
using System.Reflection;
namespace MyProgram
{
/// <summary>
/// Class with program entry point.
/// </summary>
internal sealed class Program
{
/// <summary>
/// Program entry point.
/// </summary>
///
public struct Config {
public int Guns;
public int RedGunMin;
public int RedGunMax;
public int X;
public int Y;
public double PointRatio;
}
public static Config TC;
public static int OffsetX = 0;
public static int OffsetY = 0;
public static int Offset = 0;
public static int FirstPass = 0;
public static int XC = 0;
public static int YC = 0;
public static int RedVal = 0;
public static int GreenVal = 0;
public static int BlueVal = 0;
//Set up the random number seed
public static Random rand = new Random();
//Create a bitmap object (from the image in the resource file)
//***** The following line of code (70) generates the error: *****
public static System.Drawing.Bitmap myBitMap = new Bitmap(MyProgram.Resource1.Black600x600,Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y);
private static System.Timers.Timer myTimer;
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
string firstArgument = args[0].ToLower().Trim();
if (firstArgument == "/c") // Configuration mode
{
// TODO
}
else //(firstArgument == "/s") Full-screen mode, and default mode
{
//Read the config file
string strDocFolder;
TileConfig TC = new TileConfig();
strDocFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//Check to see if the INI file exists
if (File.Exists(strDocFolder + "\\TileConfig.ini"))
{
//Read the configuration out of the file
TextReader TR = new StreamReader(strDocFolder + "\\TileConfig.ini");
TC.Guns = Convert.ToInt32(TR.ReadLine());
TC.RedGunMin = Convert.ToInt32(TR.ReadLine());
TC.RedGunMax = Convert.ToInt32(TR.ReadLine());
TC.X = Convert.ToInt32(TR.ReadLine());
TC.Y = Convert.ToInt32(TR.ReadLine());
TC.PointRatio = Convert.ToDouble(TR.ReadLine());
TR.Dispose();
}
else
{
//File does not exist, assign default values
TC.Guns = 3;
TC.RedGunMin = 0;
TC.RedGunMax = 255;
TC.X = 4;
TC.Y = 3;
TC.PointRatio = 0.10;
}
//Choose initial color
RedVal = rand.Next(0, 255);
ShowScreen();
myTimer.Start();
Application.Run();
}
}
}
當我異常詳細信息複製到剪貼板,這是我所得到的:
System.TypeInitializationException was unhandled
HResult=-2146233036
Message=The type initializer for 'MyProgram.Program' threw an exception.
Source=MyProgram
TypeName=MyProgram.Program
StackTrace:
at MyProgram.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ArgumentException
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height)
at MyProgram.Program..cctor() in C:\Users\Randy\Documents\SharpDevelop Projects\MyProgram\MyProgram\Program.cs:line 70
InnerException:
感謝您能提供任何援助。你們是最棒的,我真的很欣賞你們所有人的幫助!
謝謝你的快速幫助!並且爲了您耐心的與老黑客誰經常閱讀他自己的意義參數,而不是徹底閱讀文檔! – HandyRandyIndy