龙的天空
WinForm C# Web .NET

最近学习C#编程,想自己编些桌面小程序玩玩,接触到了线程,GDI+,网络文件传输一系列的东东。编这些程序前我已经知道这水很深,可是接触了,才发现这水这么这么深,会淹死人的……几乎每天都是从早到晚的盯着MSDN,这个连接跳到那个连接,那个连接又跳回这个连接,那个寒啊……

言归正传,在编程的时候发现了一个问题,关于绘制Image.FromStream()所产生的图片时候会出现内存不足的警告。而Image.FromFile()着根本不会出现这问题,实在是很不解啊。

现将代码公布如下,给大家参考,希望大家可以告诉我,这是为什么。。。难道这就是传说中的BUG~~我运气也太好了点呀。。。。微软是不是要发点奖金给我。。呵呵。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;

namespace Project1
{
    
public partial class Clock : Form
    
{
        
//定义一个Image
        Image image1;
        
//旋转角度,值为90和180的时候绘制会产生内存不足警告
        float float1 = 88;

        
//定义128*128图象的初始点
        Point[] PT = new Point[] new Point(-64-64), new Point(64-64), new Point(-6464) };

        
public Clock()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
this.Refresh();
            float1
++;
            label1.Text 
= float1.ToString();
            System.Drawing.Graphics PicGraphics 
= this.pictureBox1.CreateGraphics();
            
//把64,64点设置为中心点,将以这个中心点为圆心旋转
            PicGraphics.TranslateTransform(6464);
            GraphicsState gs 
= PicGraphics.Save();
            PicGraphics.RotateTransform(float1);
            PicGraphics.DrawImage(image1, PT);
            PicGraphics.Restore(gs);
            PicGraphics.Dispose();
        }


        
private void Clock_Load(object sender, EventArgs e)
        
{
            label1.Text 
= float1.ToString();

            
//读取一个128*128的PNG文件
            byte[] nbytes = new byte[(new FileInfo("a.png")).Length];
            
using (FileStream fs = new FileStream("a.png", FileMode.Open, FileAccess.Read, FileShare.Read, 1true))
            
{
                fs.Read(nbytes, 
0, nbytes.Length);
                
using (Stream stream = new MemoryStream(nbytes))
                
{
                    image1 
= Image.FromStream(stream);

                }

            }


        }


        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.PictureBox pictureBox1;
        
private System.Windows.Forms.Label label1;

        
private void InitializeComponent()
        
{
            
this.button1 = new System.Windows.Forms.Button();
            
this.pictureBox1 = new System.Windows.Forms.PictureBox();
            
this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(
this.pictureBox1)).BeginInit();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(1212);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(7523);
            
this.button1.TabIndex = 0;
            
this.button1.Text = "增加1角度";
            
this.button1.UseVisualStyleBackColor = true;
            
this.button1.Click += new System.EventHandler(this.button1_Click);
            
// 
            
// pictureBox1
            
// 
            this.pictureBox1.Location = new System.Drawing.Point(7365);
            
this.pictureBox1.Name = "pictureBox1";
            
this.pictureBox1.Size = new System.Drawing.Size(128128);
            
this.pictureBox1.TabIndex = 1;
            
this.pictureBox1.TabStop = false;
            
// 
            
// label1
            
// 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(12522);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(1712);
            
this.label1.TabIndex = 2;
            
            
// 
            
// Clock
            
// 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
this.ClientSize = new System.Drawing.Size(292266);
            
this.Controls.Add(this.label1);
            
this.Controls.Add(this.pictureBox1);
            
this.Controls.Add(this.button1);
            
this.Name = "Clock";
            
this.Text = "Clock";
            
this.Load += new System.EventHandler(this.Clock_Load);
            ((System.ComponentModel.ISupportInitialize)(
this.pictureBox1)).EndInit();
            
this.ResumeLayout(false);
            
this.PerformLayout();

        }


        
static class Program
        
{
            
/// <summary>
            
/// 应用程序的主入口点。
            
/// </summary>

            [STAThread]
            
static void Main()
            
{
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
false);
                Application.Run(
new Clock());
            }

        }


    }

}
posted on 2007-06-04 17:41  龙的天空  阅读(3835)  评论(2)    收藏  举报