Moduler>GUI-komponenter>FileLoad
Last opp tekst og bilde
Vi lager det nytt "project" av typen "Windows Application". Så bruker vi GUI-editoren til å lage en enkel form. Vi får igjen tre kildefiler:
- Program.cs
- Form1.cs
- Form1.Designer.cs
Vi konsentrerer oss om den ene fila som inneholder vår handskrevne kode
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D
;
namespace control7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.FileName="";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox1.Text = ReadTextFile(openFileDialog1.FileName);
}
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.Filter = "gif files (*.gif)|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//pictureBox1.ImageLocation = openFileDialog1.FileName;
Image im = Image.FromFile(openFileDialog1.FileName);
Image im2 = FixedSize(im, pictureBox1.Width, pictureBox1.Height);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
//pictureBox1.Image = im;
pictureBox1.Image = im2;
}
}
private String ReadTextFile(String filename)
{
StreamReader sr=null;
String result = "";
try
{
sr = new StreamReader(
new FileStream(filename,
FileMode.Open,
FileAccess.Read));
result = sr.ReadToEnd();
}
catch (Exception ex)
{
result= ex.Message;
}
finally
{
if ( sr !=null)
sr.Close();
}
return result;
}
static Image FixedSize(Image imgPhoto, int Width, int Height)
{
// one of many ways to resize an image
// taken from :http://www.codeproject.com/csharp/imageresize.asp
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
// something to fill with:
// grPhoto.Clear(Color.Red);
grPhoto.Clear(System.Drawing.SystemColors.Control);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
}












