You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
7.3 KiB
194 lines
7.3 KiB
using ESRI.ArcGIS.Carto; |
|
using ESRI.ArcGIS.Display; |
|
using ESRI.ArcGIS.esriSystem; |
|
using System; |
|
using System.Drawing; |
|
using System.Drawing.Drawing2D; |
|
|
|
namespace Kingo.Plugin.EngineEditor.Common |
|
{ |
|
public class BitmapScaleHelper |
|
{ |
|
|
|
/// <summary> |
|
/// 缩放图片 |
|
/// </summary> |
|
/// <param name="bitmap">原图片</param> |
|
/// <param name="width">新图片宽度</param> |
|
/// <param name="height">新图片高度</param> |
|
/// <returns>新图片</returns> |
|
public Bitmap ScaleToSize(Bitmap bitmap, int width, int height) |
|
{ |
|
if (bitmap.Width == width && bitmap.Height == height) |
|
{ |
|
return bitmap; |
|
} |
|
|
|
var scaledBitmap = new Bitmap(width, height); |
|
using (var g = Graphics.FromImage(scaledBitmap)) |
|
{ |
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic; |
|
g.DrawImage(bitmap, 0, 0, width, height); |
|
} |
|
|
|
return scaledBitmap; |
|
} |
|
|
|
/// <summary> |
|
/// 缩放图片 |
|
/// </summary> |
|
/// <param name="bitmap">原图片</param> |
|
/// <param name="size">新图片大小</param> |
|
/// <returns>新图片</returns> |
|
public Bitmap ScaleToSize(Bitmap bitmap, Size size) |
|
{ |
|
//return bitmap.ScaleToSize(size.Width, size.Height); |
|
return ScaleToSize(bitmap, size.Width, size.Height); |
|
} |
|
|
|
/// <summary> |
|
/// 按比例来缩放 |
|
/// </summary> |
|
/// <param name="bitmap">原图</param> |
|
/// <param name="ratio">ratio大于1,则放大;否则缩小</param> |
|
/// <returns>新图片</returns> |
|
public Bitmap ScaleToSize(Bitmap bitmap, float ratio) |
|
{ |
|
return ScaleToSize(bitmap, (int)(bitmap.Width * ratio), (int)(bitmap.Height * ratio)); |
|
} |
|
|
|
/// <summary> |
|
/// 按给定长度/宽度等比例缩放 |
|
/// </summary> |
|
/// <param name="bitmap">原图</param> |
|
/// <param name="width">新图片宽度</param> |
|
/// <param name="height">新图片高度</param> |
|
/// <returns>新图片</returns> |
|
public Bitmap ScaleProportional(Bitmap bitmap, int width, int height) |
|
{ |
|
float proportionalWidth, proportionalHeight; |
|
|
|
if (width.Equals(0)) |
|
{ |
|
proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width; |
|
proportionalHeight = height; |
|
} |
|
else if (height.Equals(0)) |
|
{ |
|
proportionalWidth = width; |
|
proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height; |
|
} |
|
else if (((float)width) / bitmap.Size.Width * bitmap.Size.Height <= height) |
|
{ |
|
proportionalWidth = width; |
|
proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height; |
|
} |
|
else |
|
{ |
|
proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width; |
|
proportionalHeight = height; |
|
} |
|
|
|
return ScaleToSize(bitmap, (int)proportionalWidth, (int)proportionalHeight); |
|
} |
|
|
|
/// <summary> |
|
/// 按给定长度/宽度缩放,同时可以设置背景色 |
|
/// </summary> |
|
/// <param name="bitmap">原图</param> |
|
/// <param name="backgroundColor">背景色</param> |
|
/// <param name="width">新图片宽度</param> |
|
/// <param name="height">新图片高度</param> |
|
/// <returns></returns> |
|
public Bitmap ScaleToSize(Bitmap bitmap, Color backgroundColor, int width, int height) |
|
{ |
|
var scaledBitmap = new Bitmap(width, height); |
|
using (var g = Graphics.FromImage(scaledBitmap)) |
|
{ |
|
g.Clear(backgroundColor); |
|
var proportionalBitmap = ScaleProportional(bitmap, width, height); |
|
var imagePosition = new Point((int)((width - proportionalBitmap.Width) / 2m), (int)((height - proportionalBitmap.Height) / 2m)); |
|
g.DrawImage(proportionalBitmap, imagePosition); |
|
} |
|
return scaledBitmap; |
|
} |
|
/// <summary> |
|
/// 符号转化为图片 |
|
/// </summary> |
|
/// <param name="pSymbol">符号</param> |
|
/// <param name="width">图片的宽</param> |
|
/// <param name="height">图片的高</param> |
|
/// <returns></returns> |
|
public Bitmap PreViewFillSymbol(ISymbol pSymbol, int width, int height) |
|
{ |
|
IStyleGalleryClass pStyleGalleryClass = new FillSymbolStyleGalleryClass(); |
|
Bitmap img = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); |
|
Graphics gc = Graphics.FromImage(img); |
|
IntPtr hdc = gc.GetHdc(); |
|
tagRECT rect = new tagRECT(); |
|
rect.left = 0; |
|
rect.top = 0; |
|
rect.right = width; |
|
rect.bottom = height; |
|
pStyleGalleryClass.Preview(pSymbol, hdc.ToInt32(), ref rect); |
|
//gc.Save(); |
|
gc.ReleaseHdc(hdc); |
|
gc.Dispose(); |
|
img.MakeTransparent(Color.FromArgb(SystemColors.Window.ToArgb())); |
|
return img.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
|
} |
|
/// <summary> |
|
/// 截取bitmap图像,将图像分成多分,如果selected为空就全部返回 |
|
/// </summary> |
|
/// <param name="bmpRes"></param> |
|
/// <param name="nXClipNum">截取行数</param> |
|
/// <param name="nYClipNum">截取列数</param> |
|
/// <param name="selected">选着截取后的第几个,从0开始算</param> |
|
public Bitmap[] PhotoScreenShot(Bitmap bmpRes, int nXClipNum, int nYClipNum, int[] selected) |
|
{ |
|
Bitmap[] bmpaClipBmpArr = new Bitmap[nYClipNum * nXClipNum]; |
|
try |
|
{ |
|
for (int nYClipNumIndex = 0; nYClipNumIndex < nYClipNum; nYClipNumIndex++) |
|
{ |
|
for (int nXClipNumIndex = 0; nXClipNumIndex < nXClipNum; nXClipNumIndex++) |
|
{ |
|
int nClipWidth = bmpRes.Width / nXClipNum; |
|
int nClipHight = bmpRes.Height / nYClipNum; |
|
int nBmpIndex = nXClipNumIndex + nYClipNumIndex * nYClipNum; |
|
|
|
Rectangle rClipRect = new Rectangle( |
|
nClipWidth * nXClipNumIndex, |
|
nClipHight * nYClipNumIndex, |
|
nClipWidth, |
|
nClipHight); |
|
|
|
bmpaClipBmpArr[nBmpIndex] = bmpRes.Clone(rClipRect, bmpRes.PixelFormat); |
|
} |
|
} |
|
if (selected == null || selected.Length == 0) |
|
{ |
|
return bmpaClipBmpArr; |
|
} |
|
else |
|
{ |
|
Bitmap[] temp = new Bitmap[selected.Length]; |
|
int j = 0; |
|
for (int i = 0; i < selected.Length; i++) |
|
{ |
|
temp[j++] = bmpaClipBmpArr[selected[i]]; |
|
} |
|
return temp; |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
throw ex; |
|
} |
|
finally |
|
{ |
|
|
|
} |
|
} |
|
} |
|
}
|
|
|