Mattyfog
17-09-2008, 18:34
Ciao,
ho preso da pierotofy.it il codice sorgente di un programma in C# che salva uno screenshot. Il problemma è che nella riga screenshot.Save(t.Text); ho questo errore durante l'esecuzione del programma Errore generico in GDI+
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Screenshot
{
/// <summary>
/// Descrizione di riepilogo per Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox t;
private System.Windows.Forms.SaveFileDialog sf;
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Necessario per il supporto di Progettazione Windows Form
//
InitializeComponent();
//
// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
//
}
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.t = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.sf = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(280, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Scatta Screenshot";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Cartella su cui salvare lo screenshot";
//
// t
//
this.t.Location = new System.Drawing.Point(8, 24);
this.t.Name = "t";
this.t.ReadOnly = true;
this.t.Size = new System.Drawing.Size(256, 23);
this.t.TabIndex = 2;
this.t.Text = "C:\\screenshot.bmp";
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(24, 24);
this.button2.TabIndex = 3;
this.button2.Text = "...";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// sf
//
this.sf.Filter = "File bitmap (.bmp)|*.bmp";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
this.ClientSize = new System.Drawing.Size(292, 86);
this.Controls.Add(this.button2);
this.Controls.Add(this.t);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Font = new System.Drawing.Font("Comic Sans MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Screenshot by Dokkis";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Il punto di ingresso principale dell'applicazione.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public static Bitmap ScreenShot()
{
SendKeys.SendWait("^{PRTSC}"); // serve a simulare la pressione del tasto stamp della tastiera
IDataObject iData = Clipboard.GetDataObject();
if ((iData.GetDataPresent(DataFormats.Bitmap)))
{
Bitmap memoryImage = new Bitmap(((Bitmap)(iData.GetData("System.Drawing.Bitmap"))));
return memoryImage;
}
return null;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.WindowState=FormWindowState.Minimized; //minimizzo sennò nello screenshot si vede il programma al centro dello schermo (cosa inutile)
Bitmap screenshot = ScreenShot();
if(screenshot!=null)
{
screenshot.Save(t.Text);
}
this.WindowState=FormWindowState.Normal; //screenshot scattato e salvato, ripristiniamo la finestra del form come quando lanciato il programma
}
private void button2_Click(object sender, System.EventArgs e)
{
sf.ShowDialog();
t.Text=sf.FileName;
}
}
}
ho preso da pierotofy.it il codice sorgente di un programma in C# che salva uno screenshot. Il problemma è che nella riga screenshot.Save(t.Text); ho questo errore durante l'esecuzione del programma Errore generico in GDI+
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Screenshot
{
/// <summary>
/// Descrizione di riepilogo per Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox t;
private System.Windows.Forms.SaveFileDialog sf;
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Necessario per il supporto di Progettazione Windows Form
//
InitializeComponent();
//
// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
//
}
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.t = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.sf = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(280, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Scatta Screenshot";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Cartella su cui salvare lo screenshot";
//
// t
//
this.t.Location = new System.Drawing.Point(8, 24);
this.t.Name = "t";
this.t.ReadOnly = true;
this.t.Size = new System.Drawing.Size(256, 23);
this.t.TabIndex = 2;
this.t.Text = "C:\\screenshot.bmp";
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(24, 24);
this.button2.TabIndex = 3;
this.button2.Text = "...";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// sf
//
this.sf.Filter = "File bitmap (.bmp)|*.bmp";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
this.ClientSize = new System.Drawing.Size(292, 86);
this.Controls.Add(this.button2);
this.Controls.Add(this.t);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Font = new System.Drawing.Font("Comic Sans MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Screenshot by Dokkis";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Il punto di ingresso principale dell'applicazione.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public static Bitmap ScreenShot()
{
SendKeys.SendWait("^{PRTSC}"); // serve a simulare la pressione del tasto stamp della tastiera
IDataObject iData = Clipboard.GetDataObject();
if ((iData.GetDataPresent(DataFormats.Bitmap)))
{
Bitmap memoryImage = new Bitmap(((Bitmap)(iData.GetData("System.Drawing.Bitmap"))));
return memoryImage;
}
return null;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.WindowState=FormWindowState.Minimized; //minimizzo sennò nello screenshot si vede il programma al centro dello schermo (cosa inutile)
Bitmap screenshot = ScreenShot();
if(screenshot!=null)
{
screenshot.Save(t.Text);
}
this.WindowState=FormWindowState.Normal; //screenshot scattato e salvato, ripristiniamo la finestra del form come quando lanciato il programma
}
private void button2_Click(object sender, System.EventArgs e)
{
sf.ShowDialog();
t.Text=sf.FileName;
}
}
}