跟据9png的实现原理自己写了个生成图片的函数,9png的原理是将图片切成9块如下

其中1、3、7、9不进行缩放,2,4,5,6,8进行缩放,这样就防止了放大后导致边界出现锯齿的问题

在实现过程中主要的就是找到4个关键点,如下

然后根据p1,p2,p3,p4将原图画到新大小的图上

具体代码如下

获得关键点

/// <summary>
/// 获取4个关键坐标点左边1,2 上边1,2
/// </summary>
/// <param name="bitmap">图片</param>
/// <param name="backColor">背景色</param>
/// <param name="intAalpha">透明度</param>
/// <returns>0:左1 1:左2 2:上1 3:上2</returns>
private Point[] GetIndex(Bitmap bitmap, Color? backColor = null, int intAalpha = )
{
int intTop = ;
int intRight = ;
int intBottom = ;
int intLeft = ; Point ptop1 = Point.Empty, ptop2 = Point.Empty;
Point pleft1 = Point.Empty, pleft2 = Point.Empty;
Point pbottom1 = Point.Empty, pbottom2 = Point.Empty;
Point pright1 = Point.Empty, pright2 = Point.Empty;
#region 边界
//left
for (int x = ; x < bitmap.Width; x++)
{
for (int y = ; y < bitmap.Height; y++)
{
Color c = bitmap.GetPixel(x, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
intLeft = x;
x = int.MaxValue - ;
break;
}
} //right
for (int x = bitmap.Width - ; x >= ; x--)
{
for (int y = ; y < bitmap.Height; y++)
{
Color c = bitmap.GetPixel(x, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
intRight = x;
x = -;
break;
}
} //top
for (int y = ; y < bitmap.Height; y++)
{
for (int x = ; x < bitmap.Width; x++)
{
Color c = bitmap.GetPixel(x, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
intTop = y;
y = int.MaxValue - ;
break;
}
} //bottom
for (int y = bitmap.Height - ; y >= ; y--)
{
for (int x = ; x < bitmap.Width; x++)
{
Color c = bitmap.GetPixel(x, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
intBottom = y;
y = -;
break;
}
}
#endregion #region 顶点
bool blnGet1 = false;
bool blnGet2 = false;
//上1 下1
for (int x = ; x < bitmap.Width; x++)
{
//上1
if (!blnGet1)
{
Color c = bitmap.GetPixel(x, intTop);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
ptop1 = new Point(x, intTop);
blnGet1 = true;
}
//下1
if (!blnGet2)
{
Color c = bitmap.GetPixel(x, intBottom);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pbottom1 = new Point(x, intBottom);
blnGet2 = true;
}
if (blnGet1 && blnGet2)
{
break;
}
} blnGet1 = false;
blnGet2 = false;
//上2 下2
for (int x = bitmap.Width - ; x >= ; x--)
{
//上2
if (!blnGet1)
{
Color c = bitmap.GetPixel(x, intTop);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
ptop2 = new Point(x, intTop);
blnGet1 = true;
}
//下2
if (!blnGet2)
{
Color c = bitmap.GetPixel(x, intBottom);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pbottom2 = new Point(x, intBottom);
blnGet2 = true;
}
if (blnGet1 && blnGet2)
{
break;
}
} blnGet1 = false;
blnGet2 = false;
//左1 右1
for (int y = ; y < bitmap.Height; y++)
{
//左1
if (!blnGet1)
{
Color c = bitmap.GetPixel(intLeft, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pleft1 = new Point(intLeft, y);
blnGet1 = true;
}
//右1
if (!blnGet2)
{
Color c = bitmap.GetPixel(intRight, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pright1 = new Point(intRight, y);
blnGet2 = true;
}
if (blnGet1 && blnGet2)
{
break;
}
} blnGet1 = false;
blnGet2 = false;
//左2 右2
for (int y = bitmap.Height - ; y >= ; y--)
{
//左2
if (!blnGet1)
{
Color c = bitmap.GetPixel(intLeft, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pleft2 = new Point(intLeft, y);
blnGet1 = true;
}
//右2
if (!blnGet2)
{
Color c = bitmap.GetPixel(intRight, y);
if (c.A <= intAalpha)
continue;
if (backColor.HasValue && c.ToArgb() == backColor.Value.ToArgb())
{
continue;
}
pright2 = new Point(intRight, y);
blnGet2 = true;
}
if (blnGet1 && blnGet2)
{
break;
}
}
#endregion Point pRleft1, pRleft2;
Point pRtop1, pRtop2;
int intLeftMax = Math.Max(ptop1.X, pbottom1.X);
int intRightMin = Math.Min(ptop2.X, pbottom2.X);
int intTopMax = Math.Max(pleft1.Y, pright1.Y);
int intBottomMin = Math.Min(pleft2.Y, pright2.Y); if (intRightMin - intLeftMax ==)
{
intRightMin = intRightMin + ;
}
else if (intRightMin - intLeftMax > )
{
intRightMin = intRightMin - ;
intLeftMax = intLeftMax + ;
} if (intBottomMin - intTopMax == )
{
intBottomMin = intBottomMin + ;
}
else if (intBottomMin - intTopMax > )
{
intBottomMin = intBottomMin - ;
intTopMax = intTopMax + ;
} pRleft1 = new Point(intLeft, intTopMax);
pRleft2 = new Point(intLeft, intBottomMin);
pRtop1 = new Point(intLeftMax, intTop);
pRtop2 = new Point(intRightMin, intTop);
Point[] ps = new Point[];
ps[] = pRleft1;
ps[] = pRleft2;
ps[] = pRtop1;
ps[] = ptop2;
return ps;
}

画图

 private Bitmap CreateBitmap(Bitmap bitmap, Point[] ps, Size size)
{
Bitmap _returnBitmap = new Bitmap(size.Width, size.Height);
Graphics g = Graphics.FromImage(_returnBitmap);
//左上角
Rectangle destRect = new Rectangle(new Point(, ), new Size(ps[].X, ps[].Y));
g.DrawImage(bitmap, destRect, , , ps[].X, ps[].Y, GraphicsUnit.Pixel);
//右上角
destRect = new Rectangle(new Point(size.Width - (bitmap.Width - ps[].X), ps[].Y), new Size(bitmap.Width - ps[].X, ps[].Y));
g.DrawImage(bitmap, destRect, ps[].X, ps[].Y, bitmap.Width - ps[].X, ps[].Y, GraphicsUnit.Pixel);
//左下角
destRect = new Rectangle(new Point(, size.Height - (bitmap.Height - ps[].Y)), new Size(ps[].X, bitmap.Height - ps[].Y));
g.DrawImage(bitmap, destRect, , ps[].Y, ps[].X, (bitmap.Height - ps[].Y), GraphicsUnit.Pixel);
//右下角
destRect = new Rectangle(new Point(size.Width - (bitmap.Width - ps[].X), size.Height - (bitmap.Height - ps[].Y)), new Size(bitmap.Width - ps[].X, bitmap.Height - ps[].Y));
g.DrawImage(bitmap, destRect, ps[].X, ps[].Y, bitmap.Width - ps[].X, bitmap.Height - ps[].Y, GraphicsUnit.Pixel);
//上中
destRect = new Rectangle(new Point(ps[].X, ), new Size(size.Width - ps[].X - (bitmap.Width - ps[].X), ps[].Y));
g.DrawImage(bitmap, destRect, ps[].X, , ps[].X - ps[].X, ps[].Y, GraphicsUnit.Pixel);
//下中
destRect = new Rectangle(new Point(ps[].X, size.Height - (bitmap.Height - ps[].Y)), new Size(size.Width - ps[].X - (bitmap.Width - ps[].X), bitmap.Height - ps[].Y));
g.DrawImage(bitmap, destRect, ps[].X, ps[].Y, ps[].X - ps[].X, bitmap.Height - ps[].Y, GraphicsUnit.Pixel);
//左中
destRect = new Rectangle(new Point(, ps[].Y), new Size(ps[].X, size.Height - ps[].Y - (bitmap.Height - ps[].Y)));
g.DrawImage(bitmap, destRect, , ps[].Y, ps[].X, ps[].Y-ps[].Y, GraphicsUnit.Pixel);
//右中
destRect = new Rectangle(new Point(size.Width - (bitmap.Width - ps[].X), ps[].Y), new Size(bitmap.Width - ps[].X, size.Height - ps[].Y - (bitmap.Height - ps[].Y)));
g.DrawImage(bitmap, destRect, ps[].X, ps[].Y, bitmap.Width-ps[].X, ps[].Y - ps[].Y, GraphicsUnit.Pixel);
//中中
destRect = new Rectangle(new Point(ps[].X, ps[].Y), new Size(size.Width - ps[].X - (bitmap.Width - ps[].X), size.Height - ps[].Y - (bitmap.Height - ps[].Y)));
g.DrawImage(bitmap, destRect, ps[].X, ps[].Y, ps[].X - ps[].X, ps[].Y - ps[].Y, GraphicsUnit.Pixel);
g.Dispose();
return _returnBitmap;
}

使用

 string strPath = @"D:\work-hzh\code\cy_hcmzc_v10\Km.PosZC\Km.PosZC\Resources\internet+.png";
Bitmap bitmap = new Bitmap(strPath);
Point[] ps = GetIndex(bitmap,Color.White);
Bitmap br = CreateBitmap(bitmap, ps, new Size(, ));
br.Save("d:\\123.png");

测试原图(100*100)

放大后(400*400)

这个是随便找的图,一般在应用中,是纯色活渐变色的会比较好,中间有图案的话 效果就不是太好了

c# 9png实现(图片缩放)的更多相关文章

  1. CSS实现图片缩放特效

    今天是感恩节,祝大家感恩节快乐哦!最近天冷了,大家注意保暖哟.下面一起看看小颖写的demo吧. html代码: <!DOCTYPE html> <html> <head& ...

  2. HTML5 图片缩放功能

    腾讯新闻上用的插件(xw.qq.com) 缩放插件scale.js (function(window, undefined) { var document = window.document, sup ...

  3. PHP图片裁剪_图片缩放_PHP生成缩略图

    在制作网页过程中,为了排版整齐美观,对网页中的图片处理成固定大小尺寸的图片,或是要截去图片边角中含有水印的图片,对于图片量多,每天更新大量图,靠人工PS处理是不现实的,那么有没有自动处理图片的程序了! ...

  4. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  5. UISlider显示进度(并且实现图片缩放)

    图片展示效果如下: 其他没什么好说的,直接上代码: RootView.h: #import <UIKit/UIKit.h> @interface RootView : UIView @pr ...

  6. Android图片缩放方法

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  7. Android安卓开发中图片缩放讲解

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  8. Asp.net 实现图片缩放 无水印(方法一)

    /// <summary> /// 图片缩放 无水印 /// </summary> /// <param name="sourceFile">图 ...

  9. android关于图片缩放

    网上有许多关于图片缩放的demo,本人都感觉不怎么好用,最近在github看到了 一个简单的支持多指缩放图片的Android View类 gesture-imageview (地址:https://g ...

  10. UIScrollView 之图片缩放

    UIScrollView 之图片缩放 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理 也就是说,要完成缩放功能的话,只 ...

随机推荐

  1. 使用ant编译项目技能

    ant编译时指定jdk的版本号 系统的jdk版本号是1.6,而项目使用的jdk版本号是1.5.所以在编译时须要指定jdk的版本号为1.5,能够使用以下的方法为javac 任务指定fork和execut ...

  2. Linux下用GCC

    Linux下用GCC 前言 离职前对做过的支付系统进行了一番#总结,继续完善我的C服务器. 本想着接下来大概实现一下 CGI 协议,但是实现过程中被一个问题卡住了: C进程与php进程的交互数据类型问 ...

  3. 你不从地址栏中增加曝光量所需的数据库ID方法

    <p><span style="font-size: 18px;"></span></p> 当你想隐藏数据库id时,你能够使用 Ha ...

  4. /var/tmp/.oracle 和 oracle listener (监听)的一点理解

    关于 /var/tmp/.oracle 的作用測试 ~---查看 /var/tmp 的权限 [root@lixora var]# ll total 164 ... drwxrwxrwt  3 root ...

  5. Qt控件焦点切换

    们日常切换控件,例如QQ登陆的账号和密码输入框就可以通过Tab键切换焦点.  图1 qq切换焦点 Qt中QWidget提供了一个静态方式实现该效果 其中也包含介绍使用 [static] void QW ...

  6. Linux性能测试 netstat命令

    功能说明:Netstat用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.语 法:netstat [-acCeFghilMnNoprstuvVwx][- ...

  7. [Songqw.Net 基础]WPF插件化中同步Style

    原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...

  8. Oracle使用dblink连接SqlServer

    使用场景:当你需要从ORACLE数据库上访问另一台SqlServer数据库的数据时,Oracle提供了一个工具:gateways.通过这个工具,你可以创建dblink来连接sqlserver或其他不同 ...

  9. Qt移动应用开发(六):QML与C++互动

    Qt移动应用开发(六):QML与C++互动 上一篇文章讲到了在Qt Quick中实现场景切换的一种可能的方法,场景切换是诸如游戏等应用在内必需要面临的技术难点,所以场景切换并没有通行的方法,依据自己的 ...

  10. Ubuntu更改 resolv.conf 重启失效

    更改Ubuntu的 resolv.conf的时候,重启的时候,经常又给重置了.google大法找到方法. sudo apt-get install resolvconf  原来Ubuntu的resol ...