我是先把图像从服务器上下到本地,然后用PictureBox进行显示,并对其放大处理,下面是用来放大的类:
public class ZoomOut
{
static public Bitmap Zoom(Bitmap source,double n)
{
Bitmap bmp =new Bitmap(Convert.ToInt32(source.Width*n),Convert.ToInt32(source.Height*n));
Graphics g = Graphics.FromImage(bmp);
Rectangle dest = new Rectangle(0,0,Convert.ToInt32(source.Width*n),Convert.ToInt32(source.Height*n));
Rectangle sour = new Rectangle(0,0,source.Width,source.Height);
g.DrawImage(source,dest,sour,System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
下面是调用其对图像放大:
private void menuItemlarge1_Click(object sender, System.EventArgs e)
{
//Bitmap bmp = (Bitmap)pictureBox1.Image;
Bitmap bmp = new Bitmap("\\_0001.bmp");
Bitmap bm = ZoomOut.Zoom(bmp,1.3);
bmp.Dispose();
this.pictureBox1.Image =bm;
this.pictureBox1.Size = bm.Size;
this.vScrollBar1.Maximum = bm.Height + this.ClientSize.Height;
this.hScrollBar1.Maximum = bm.Width + this.ClientSize.Width;
this.hScrollBar1.Value = 0;
}
private void menuItemlarge2_Click(object sender, System.EventArgs e)
{ //Bitmap bmp = (Bitmap)pictureBox1.Image;
Bitmap bmp = new Bitmap("\\_0001.bmp");
Bitmap bm = ZoomOut.Zoom(bmp,2.0);
bmp.Dispose();
this.pictureBox1.Image = bm;
this.pictureBox1.Size = bm.Size;
this.vScrollBar1.Maximum = bm.Height + this.ClientSize.Height;
this.hScrollBar1.Maximum = bm.Width + this.ClientSize.Width;
this.hScrollBar1.Value = 0;
}
private void menuItemBack_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
this.pictureBox1.Refresh();
Bitmap myBitmap= new Bitmap("\\_0001.bmp");
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.Image=(Image)myBitmap;
Cursor.Current = Cursors.Default;
}
这里运行后有两个问题:
1. 进行large1放大时,成功,但感觉不像放大了1.3倍的样子,像放大了2倍,然后接着进行large2放大,但每次到这里就提示OutOfMermoryException错误,怎么会内存溢出呢?难道是我的内存小的原因(我的是运行在智能手机上的程序)?要怎么解决呢?是我的程序的原因吗?
2. 如果我large1放大后,不进行large2放大,而是要返回原始图像(即放大前的,硬盘上的图像),但根本不行,我的程序不对吗?感觉很简单的呀?不就是把硬盘上的图像重新赋给PictureBox吗?为什么这么简单的程序却不行呢?哪里的错误呢?
上面的两个问题困扰我很长时间了,那位好心的大哥能帮帮我吗?感激涕零呀!!!
public class ZoomOut
{
static public Bitmap Zoom(Bitmap source,double n)
{
Bitmap bmp =new Bitmap(Convert.ToInt32(source.Width*n),Convert.ToInt32(source.Height*n));
Graphics g = Graphics.FromImage(bmp);
Rectangle dest = new Rectangle(0,0,Convert.ToInt32(source.Width*n),Convert.ToInt32(source.Height*n));
Rectangle sour = new Rectangle(0,0,source.Width,source.Height);
g.DrawImage(source,dest,sour,System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
下面是调用其对图像放大:
private void menuItemlarge1_Click(object sender, System.EventArgs e)
{
//Bitmap bmp = (Bitmap)pictureBox1.Image;
Bitmap bmp = new Bitmap("\\_0001.bmp");
Bitmap bm = ZoomOut.Zoom(bmp,1.3);
bmp.Dispose();
this.pictureBox1.Image =bm;
this.pictureBox1.Size = bm.Size;
this.vScrollBar1.Maximum = bm.Height + this.ClientSize.Height;
this.hScrollBar1.Maximum = bm.Width + this.ClientSize.Width;
this.hScrollBar1.Value = 0;
}
private void menuItemlarge2_Click(object sender, System.EventArgs e)
{ //Bitmap bmp = (Bitmap)pictureBox1.Image;
Bitmap bmp = new Bitmap("\\_0001.bmp");
Bitmap bm = ZoomOut.Zoom(bmp,2.0);
bmp.Dispose();
this.pictureBox1.Image = bm;
this.pictureBox1.Size = bm.Size;
this.vScrollBar1.Maximum = bm.Height + this.ClientSize.Height;
this.hScrollBar1.Maximum = bm.Width + this.ClientSize.Width;
this.hScrollBar1.Value = 0;
}
private void menuItemBack_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
this.pictureBox1.Refresh();
Bitmap myBitmap= new Bitmap("\\_0001.bmp");
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.Image=(Image)myBitmap;
Cursor.Current = Cursors.Default;
}
这里运行后有两个问题:
1. 进行large1放大时,成功,但感觉不像放大了1.3倍的样子,像放大了2倍,然后接着进行large2放大,但每次到这里就提示OutOfMermoryException错误,怎么会内存溢出呢?难道是我的内存小的原因(我的是运行在智能手机上的程序)?要怎么解决呢?是我的程序的原因吗?
2. 如果我large1放大后,不进行large2放大,而是要返回原始图像(即放大前的,硬盘上的图像),但根本不行,我的程序不对吗?感觉很简单的呀?不就是把硬盘上的图像重新赋给PictureBox吗?为什么这么简单的程序却不行呢?哪里的错误呢?
上面的两个问题困扰我很长时间了,那位好心的大哥能帮帮我吗?感激涕零呀!!!
换一个思路,你有一副图,要在屏幕上显示图像(或图像的一部分),这样你的内存需求只有一个图像的内存和一个屏幕的内存(这个可以更小)如果pictureBox只能画一个Bitmap。 你可以每次建立一个小的bitmap然后画这个小的bitmap.再准备一个小的。
看你的code g.DrawImage 应该支持这个功能。注意:如果不能一次建立一个屏幕大小的内存可能有边缘问题。这个可能用你的方法无法解决。因为你的算法是别人写的。
2。另外,为了方便,我只进行放大,没有进行缩小,想着放大了以后再把原始图像重新赋给PictureBox就可以了,客观就起到缩小的作用了,但不知问什么不行,唉
3。我那个ZoomOut.Zoom()函数也可以进行缩小?n小于1时应该就是缩小,对吧
不好意思,听不太懂您说的,难道我要自己从底层做一个独立的图像控件(类似于PictureBox的)?我是新手,唉,时间又紧,所以想在这个程序上做些许改动,但不知为什么原来可以的程序,现在为什么就不行了,很是纳闷。还望大哥多多指教,感激不尽。。
1.我现在的这种方法不行吗?我查查DrawImage
2.由于是用在手机上,图片控件的长和宽不能随意改变呀........唉
private void Form1_Load(object sender, EventArgs e)
{
System.IO.MemoryStream ms=new System.IO.MemoryStream();
System.Drawing.Image.FromFile(@"c:\test1.bmp").Save (ms,System.Drawing.Imaging.ImageFormat.Bmp);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Dispose();
ms.Close();
} private void button1_Click(object sender, EventArgs e)
{
Image img=pictureBox1.Image;
System.IO.MemoryStream ms=new System.IO.MemoryStream();
// 这里放大2倍,
System.Drawing.Bitmap bmp = new Bitmap(img,img.Width*2,img.Height*2);
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Dispose();
ms.Close();
}
{
Graphics g = e.Graphics;
g.DrawImage(...); 这里进行缩放绘制...
}实在不行..在给你一个偷懒的方法..你把PictureBox设成同图同一比例,想要的大小 . 把PictureBox放在Panel中,让Panel自动滚动...
我把这句又换成Bitmap bmp = (Bitmap)pictureBox1.Image;
然后使其可以放大缩小,并把pictureBox1的属性又换成Normal,在原图像大小上进行放大或缩小,感觉好一些了