C#、VB.NET_关于图片的缩放

VB.NET与C#都与java的语法差不多,特别是C#,与java语法就更像了,这里实现了图片高质量压缩的方法,好了闲话不多说,直接上代码:

//原始图片名称

string originalFilename = "c:\\222.jpg";
//生成的高质量图片名称
string strGoodFile = "c:\\222-small-good.jpg";

//生成的低质量图片名称

string strBadFile = "c:\\222-small-bad.jpg";
//缩小的倍数
int iScale = 3;

//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
//取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale , image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), 
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//取得原图像的普通缩略图
System.Drawing.Image img = image.GetThumbnailImage(image.Width / iScale, image.Height / iScale, null, IntPtr.Zero);
//保存普通缩略图
img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);

g.Dispose();

MessageBox.Show("生成完毕");

这段是C#写的,改成相应的VB.NET代码同样可以运行。

在Delphi中,要将BMP图像放大,可以使用以下步骤: 1. 首先,加载BMP图像文件。可以使用TBitmap组件来实现这个步骤。例如,使用LoadFromFile方法加载图像: ```Delphi var bmp: TBitmap; begin bmp := TBitmap.Create; try bmp.LoadFromFile('image.bmp'); // 进行后续操作 finally bmp.Free; end; end; ``` 2. 接下来,需要创建一个新的TBitmap对象,用于保存放大后的图像。可以使用SetSize方法设置新图像的尺寸,尺寸应为原图像尺寸的倍数: ```Delphi var enlargedBmp: TBitmap; begin enlargedBmp := TBitmap.Create; try enlargedBmp.SetSize(bmp.Width * 2, bmp.Height * 2); finally enlargedBmp.Free; end; end; ``` 3. 然后,使用双层循环遍历原图像的像素,将每个像素复制到新图像的对应位置。可以使用Canvas的Pixels属性获取和设置像素的颜色值: ```Delphi var x, y: Integer; begin for x := 0 to bmp.Width - 1 do begin for y := 0 to bmp.Height - 1 do begin enlargedBmp.Canvas.Pixels[x * 2, y * 2] := bmp.Canvas.Pixels[x, y]; enlargedBmp.Canvas.Pixels[x * 2 + 1, y * 2] := bmp.Canvas.Pixels[x, y]; enlargedBmp.Canvas.Pixels[x * 2, y * 2 + 1] := bmp.Canvas.Pixels[x, y]; enlargedBmp.Canvas.Pixels[x * 2 + 1, y * 2 + 1] := bmp.Canvas.Pixels[x, y]; end; end; end; ``` 4. 最后,保存放大后的图像。可以使用SaveToFile方法将放大后的图像保存到指定的文件中: ```Delphi begin enlargedBmp.SaveToFile('enlarged_image.bmp'); end; ``` 通过以上步骤,我们可以在Delphi中实现BMP图像的放大操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值