跟据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. zoj 1008 Gnome Tetravex

    开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> ...

  2. redis举例调用两种方式方式

    在以下的代码演示样例中.将给出两种最为经常使用的Redis命令操作方式,既普通调用方式和基于管线的调用方式. 注:在阅读代码时请留意凝视.   1 #include <stdio.h>   ...

  3. Web自动化测试(全网最给力自动化教程)

    http://www.cnblogs.com/zidonghua/p/7430083.html python+selenium自动化软件测试(第2章):WebDriver API 欢迎您来阅读和练手! ...

  4. Swift下CoreData的使用

    我之前的随笔中有写过一些iOS持久化存储的方法,包含了sqlite.解归档.沙盒存放等等.这些方式中,能够大规模存储,并保持性能的只有使用sqlite了.而这里将记录下Cocoa自身继承的数据库的存储 ...

  5. Method and system for providing security policy for linux-based security operating system

    A system for providing security policy for a Linux-based security operating system, which includes a ...

  6. Windows下静态编译Qt4

    既然是静态编译,那就要编译出来的程序不信赖于任何dll文件.首先下载qt-win-opensource-4.7.4-mingw.exe: http://get.qt.nokia.com/qt/sour ...

  7. OpenGL(二十四) VAO、VBO和着色器使用示例

    1. 新建一个工程,新建一个头文件Shader.h,内容如下: #ifndef _SHADER_H_ #define _SHADER_H_ #include <vector> #inclu ...

  8. 【读书笔记】——《A Brief History of Humankind》

    I encourage all of us, whatever our beliefs, to question the basic narratives of our world, to conne ...

  9. 使用python3的base64编解码实现字符串的简易加密解密

    import base64 copyright = 'Copyright (c) 2012 Doucube Inc. All rights reserved.' def main(): #转成byte ...

  10. ImageNet 数据集

    1. top-5 error rate ImageNet 图像通常有 1000 个可能的类别,对每幅图像你可以猜 5 次结果(即同时预测5个类别标签),当其中有任何一次预测对了,结果都算对(事实上一个 ...