跟据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. sparksql 用反射的方式将rdd转换成dataset/dataframe

    java public class ReflectionDemo { private static SparkConf conf = new SparkConf().setAppName(" ...

  2. R 语言文件读写

    1. working directory:工作目录 > getwd() > setwd("C:/data") # 设定当前工作目录 2. 读取格式化的 table &g ...

  3. C# 使用外部别名

    原文:C# 使用外部别名 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客.本文 ...

  4. C#颜色对照使用表

    这篇文章来来源于C# Color Table,这里是我翻译的中文版本,其中已经加上了我的一些理解和注释.翻译这篇文章的原因是我在写C#程序的时候发现,C#自带的颜色种类极多(详见下表),如果没有直观的 ...

  5. Matlab使用鼠标标注图像位置并返回坐标(标注图像ROI)

    代码 function ROI=LabelBox(filename) Mat=imread(filename); imshow(Mat); mouse=imrect; pos=getPosition( ...

  6. Method and apparatus for establishing IEEE 1588 clock synchronization across a network element comprising first and second cooperating smart interface converters wrapping the network element

    Apparatus for making legacy network elements transparent to IEEE 1588 Precision Time Protocol operat ...

  7. Linux下编译,要下载tar.xz,而不要下载zip,因为换行的编码不一样,对.h.cpp没有影响,但是对脚本有影响 good

    原因是 在win下编辑的时候,换行结尾是\n\r , 而在linux下 是\n,所以才会有 多出来的\r但是这个我是直接下载的官网文件解压的,没有动过啊. 破案了. linux下编译要下 .tar.x ...

  8. Method and system for public-key-based secure authentication to distributed legacy applications

    A method, a system, an apparatus, and a computer program product are presented for an authentication ...

  9. Swagger 生成API文档

    1.打开程序包管理控制台输入: Install-Package Swashbuckle 2.打开App_Start文件夹下的SwaggerConfig.cs文件找到 c.IncludeXmlComme ...

  10. SEO分享:为什么我的资源外链这么多的高品质?

    在小浪前制成的制品" [完整版]我怎么3的月800单词索引厨师主页!".之后,很多人都表示了极大的钦佩观看.顽强的运行力,真SEO它是运行顽强的力量.也有人说是吹牛吧,一天500外 ...