@ -71,6 +71,78 @@ namespace DevicesService.Common
}
}
/// <summary>
/// 文件转Base64码
/// </summary>
/// <param name="fileLocation"></param>
/// <returns></returns>
public static string ImgToBase64StringSign ( string fileLocation )
{
MemoryStream ms = new MemoryStream ( ) ;
try
{
if ( System . IO . File . Exists ( fileLocation ) )
{
Bitmap bmp = new Bitmap ( @fileLocation ) ;
//BitmapFlip(90, ref bmp);
bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Jpeg ) ;
byte [ ] arr = new byte [ ms . Length ] ;
ms . Position = 0 ;
ms . Read ( arr , 0 , ( int ) ms . Length ) ;
ms . Close ( ) ;
bmp . Dispose ( ) ;
File . Delete ( fileLocation ) ;
return Convert . ToBase64String ( arr ) ;
}
return "" ;
}
catch ( Exception ex )
{
return ex . Message ;
}
finally
{
ms . Close ( ) ;
}
}
/// <summary>
/// 文件转Base64码
/// </summary>
/// <param name="fileLocation"></param>
/// <returns></returns>
public static string ImgToBase64StringBySign ( string fileLocation )
{
MemoryStream ms = new MemoryStream ( ) ;
try
{
if ( System . IO . File . Exists ( fileLocation ) )
{
Bitmap bmp1 = new Bitmap ( fileLocation ) ;
Bitmap bmp = CutImageWhitePart ( bmp1 , 1 0 ) ; //new Bitmap(@fileLocation);
BitmapFlip ( 9 0 , ref bmp ) ;
bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Jpeg ) ;
byte [ ] arr = new byte [ ms . Length ] ;
ms . Position = 0 ;
ms . Read ( arr , 0 , ( int ) ms . Length ) ;
ms . Close ( ) ;
bmp . Dispose ( ) ;
bmp1 . Dispose ( ) ;
File . Delete ( fileLocation ) ;
return Convert . ToBase64String ( arr ) ;
}
return "" ;
}
catch ( Exception ex )
{
return ex . Message ;
}
finally
{
ms . Close ( ) ;
}
}
/// <summary>
/// 图像翻转,基于图像中心
/// </summary>
@ -321,7 +393,7 @@ namespace DevicesService.Common
if ( ! file . Name . Contains ( "_compress" ) )
{
//压缩图片
image = GetPicThumbnail ( file . FullName ) ;
image = GetPicThumbnail ( file . FullName ) ;
if ( image ! = null )
{
//压缩后的图片,图片名称增加_compress
@ -409,5 +481,174 @@ namespace DevicesService.Common
string base64String = Convert . ToBase64String ( fileBytes ) ;
return base64String ;
}
/// <summary>
/// 图片处理成电子签名大小
/// </summary>
/// <param name="originalImagePath"></param>
/// <param name="resizedImagePath"></param>
public static void ResizeImage ( string originalImagePath , string resizedImagePath )
{
int width = 7 2 ;
int height = 7 2 ;
using ( Image originalImage = Image . FromFile ( originalImagePath ) )
{
// 创建一个新的画布,在其上绘制调整大小后的图片
using ( Bitmap resizedImage = new Bitmap ( width , height ) )
{
using ( Graphics graphics = Graphics . FromImage ( resizedImage ) )
{
// 设置绘图质量
graphics . CompositingQuality = System . Drawing . Drawing2D . CompositingQuality . HighQuality ;
graphics . SmoothingMode = System . Drawing . Drawing2D . SmoothingMode . HighQuality ;
graphics . InterpolationMode = System . Drawing . Drawing2D . InterpolationMode . HighQualityBicubic ;
// 清除背景并填充白色
graphics . Clear ( Color . White ) ;
graphics . DrawImage ( originalImage , new Rectangle ( 0 , 0 , width , height ) , new Rectangle ( 0 , 0 , originalImage . Width , originalImage . Height ) , GraphicsUnit . Pixel ) ;
// 保存调整大小后的图片
resizedImage . Save ( resizedImagePath , ImageFormat . Png ) ;
}
}
}
}
/// <summary>
/// 剪去图片空余白边
/// </summary>
/// <param name="FilePath">源文件</param>
/// <param name="WhiteBarRate">保留空白边比例</param>
public static Bitmap CutImageWhitePart ( Bitmap bmp , int WhiteBarRate )
{
int top = 0 , left = 0 ;
int right = bmp . Width , bottom = bmp . Height ;
Color white = Color . White ;
//寻找最上面的标线,从左(0)到右,从上(0)到下
for ( int i = 0 ; i < bmp . Height ; i + + ) //行
{
bool find = false ;
for ( int j = 0 ; j < bmp . Width ; j + + ) //列
{
Color c = bmp . GetPixel ( j , i ) ;
if ( IsWhite ( c ) )
{
top = i ;
find = true ;
break ;
}
}
if ( find ) break ;
}
//寻找最左边的标线,从上(top位)到下,从左到右
for ( int i = 0 ; i < bmp . Width ; i + + ) //列
{
bool find = false ;
for ( int j = top ; j < bmp . Height ; j + + ) //行
{
Color c = bmp . GetPixel ( i , j ) ;
if ( IsWhite ( c ) )
{
left = i ;
find = true ;
break ;
}
}
if ( find ) break ; ;
}
//寻找最下边标线,从下到上,从左到右
for ( int i = bmp . Height - 1 ; i > = 0 ; i - - ) //行
{
bool find = false ;
for ( int j = left ; j < bmp . Width ; j + + ) //列
{
Color c = bmp . GetPixel ( j , i ) ;
if ( IsWhite ( c ) )
{
bottom = i ;
find = true ;
break ;
}
}
if ( find ) break ;
}
//寻找最右边的标线,从上到下,从右往左
for ( int i = bmp . Width - 1 ; i > = 0 ; i - - ) //列
{
bool find = false ;
for ( int j = 0 ; j < = bottom ; j + + ) //行
{
Color c = bmp . GetPixel ( i , j ) ;
if ( IsWhite ( c ) )
{
right = i ;
find = true ;
break ;
}
}
if ( find ) break ;
}
int iWidth = right - left ;
int iHeight = bottom - left ;
int blockWidth = Convert . ToInt32 ( iWidth * WhiteBarRate / 1 0 0 ) ;
bmp = Cut ( bmp , left - blockWidth , top - blockWidth , right - left + 2 * blockWidth , bottom - top + 2 * blockWidth ) ;
return bmp ;
}
/// <summary>
/// 2014.6.13 来源于网络的一个函数
/// </summary>
/// <param name="b"></param>
/// <param name="StartX"></param>
/// <param name="StartY"></param>
/// <param name="iWidth"></param>
/// <param name="iHeight"></param>
/// <returns></returns>
public static Bitmap Cut ( Bitmap b , int StartX , int StartY , int iWidth , int iHeight )
{
if ( b = = null )
{
return null ;
}
int w = b . Width ;
int h = b . Height ;
if ( StartX > = w | | StartY > = h )
{
return null ;
}
if ( StartX + iWidth > w )
{
iWidth = w - StartX ;
}
if ( StartY + iHeight > h )
{
iHeight = h - StartY ;
}
try
{
Bitmap bmpOut = new Bitmap ( iWidth , iHeight , PixelFormat . Format24bppRgb ) ;
Graphics g = Graphics . FromImage ( bmpOut ) ;
g . DrawImage ( b , new Rectangle ( 0 , 0 , iWidth , iHeight ) , new Rectangle ( StartX , StartY , iWidth , iHeight ) , GraphicsUnit . Pixel ) ;
g . Dispose ( ) ;
return bmpOut ;
}
catch
{
return null ;
}
}
/// <summary>
/// 2014.6.12 判断白色与否,非纯白色
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static bool IsWhite ( Color c )
{
if ( c . R < 2 4 5 | | c . G < 2 4 5 | | c . B < 2 4 5 )
return true ;
else return false ;
}
}
}