maketransparent的简单介绍

http://www.itjxue.com  2023-03-17 09:06  来源:未知  点击次数: 

如何用c#将一张图片均匀分割成大小相等的图片,最好有代码! 能发给我一份代码吗,谢谢

正好软糖做过这个软件^o^,切割图片并保存一堆小图片到FileName-###.png

效果图

软糖切图 - 核心代码

using?System;

using?System.Drawing;

using?System.Drawing.Imaging;

using?System.Drawing.Drawing2D;

using?System.IO;

namespace?软糖切图

{

????public?class?网格精灵

????{

????????public?string?文件地址;

????????public?string?输出目录;

????????public?string?输出后缀名?=?@".png";

????????public?static?string?命名格式?=?@"{0}_{1}_{2}";

????????public?Bitmap?原始;

????????public?Bitmap?当前;

????????public?ImageFormat?图像格式;

????????public?Graphics?画板;

????????public?Color?透明色;

????????public?Pen?网格画笔?=?new?Pen(Color.FromArgb(255,?255,?0,?255),?2);

????????public?Color?无效区域色?=?Color.White;

????????public?Brush?背景画刷?=?new?HatchBrush(HatchStyle.LightDownwardDiagonal,?

????????????Color.White,Color.LightGray);

????????public?int?OX?=?0;

????????public?int?OY?=?0;

????????public?int?W;

????????public?int?H;

????????public?int?iX;

????????public?int?iY;

????????public?bool?显示网格?=?true;

????????public?void?打开(string?文件地址)

????????{

????????????装载位图(?new?Bitmap(文件地址));

????????}

????????public?void?装载位图(Bitmap?位图)

????????{

????????????原始?=?位图;

????????????图像格式?=?ImageFormat.Png;

????????????自动调整网格();

????????????当前?=?new?Bitmap(原始,?原始.Size);

????????????画板?=?Graphics.FromImage(当前);

????????}

????????public?void?重新装载()

????????{

????????????当前?=?new?Bitmap(原始,?原始.Size);

????????????画板?=?Graphics.FromImage(当前);

????????}

????????public?void?重新绘制()

????????{

????????????if?(原始?==?null)?{?return;?}

????????????绘制位图(画板);

????????????绘制网格(画板);

????????}

????????public?void?调整网格线宽度(float?宽度)

????????{

????????????if?(宽度?=?0)?{?显示网格?=?false;?}?else?{?显示网格?=?true;?}

????????????网格画笔.Width?=?宽度;

????????????重新绘制();

????????}

????????private?void?自动调整网格()

????????{

????????????if?(原始.Width??W)?{?W?=?原始.Width;?}

????????????if?(原始.Height??H)?{?H?=?原始.Height;?}

????????????//计算格子数量

????????????iX?=?(int)((原始.Width?-?OX)?/?W);

????????????iY?=?(int)((原始.Height?-?OY)?/?H);

????????}

????????private?void?绘制位图(Graphics?v画板)

????????{

????????????v画板.Clear(无效区域色);

????????????if?(当前?==?null)?{?return;?}

????????????Rectangle?RECT?=?new?Rectangle(0,?0,?当前.Width,?当前.Height);

????????????v画板.FillRectangle(背景画刷,?RECT);

????????????v画板.DrawImage(原始,?RECT);

????????}

????????private?void?绘制网格(Graphics?v画板)

????????{

????????????//检查位图和格子宽高

????????????if?(当前?==?null)?{?return;?}

????????????if?(显示网格?==?false)?{?return;?}

????????????if?(W?=?0)?{?W?=?32;?}

????????????if?(H?=?0)?{?H?=?32;?}

????????????//计算格子数量

????????????iX?=?(int)((当前.Width?-?OX)?/?W);

????????????iY?=?(int)((当前.Height?-?OY)?/?H);

????????????//检查格子数量

????????????if?(iX??1)?{?return;?}

????????????if?(iY??1)?{?return;?}

????????????//绘制网格线

????????????int?x1,?x2,?y1,?y2;

????????????for?(int?x?=?1;?x?=?iX?+?1;?x++)

????????????{

????????????????x1?=?(x?-?1)?*?W?-?OX;

????????????????y1?=?0;

????????????????x2?=?x1;

????????????????y2?=?当前.Height;

????????????????v画板.DrawLine(网格画笔,?x1,?y1,?x2,?y2);

????????????}

????????????for?(int?y?=?1;?y?=?iY?+?1;?y++)

????????????{

????????????????x1?=?0;

????????????????y1?=?(y?-?1)?*?H?-?OY;

????????????????x2?=?当前.Width;

????????????????y2?=?y1;

????????????????v画板.DrawLine(网格画笔,?x1,?y1,?x2,?y2);

????????????}

????????}

????????public?Color?取色(int?x,int?y)

????????{

????????????if?(当前?==?null){?return?Color.Black;?}

????????????return?当前.GetPixel(x,?y);

????????}

????????public?void?开关透明(bool?透明)

????????{

????????????if?(当前?==?null)?{?return;?}

????????????//重新装载();

????????????if?(透明)

????????????{

????????????????当前.MakeTransparent(透明色);

????????????}

????????????重新绘制();

????????}

????????public?void?裁剪()

????????{

????????????//检查位图和格子宽高

????????????if?(当前?==?null)?{?return;?}

????????????if?(W?=?0)?{?W?=?32;?}

????????????if?(H?=?0)?{?H?=?32;?}

????????????//计算格子数量

????????????iX?=?(int)((当前.Width?-?OX)?/?W);

????????????iY?=?(int)((当前.Height?-?OY)?/?H);

????????????//检查格子数量

????????????if?(iX?=?1)?{?return;?}

????????????if?(iY?=?1)?{?return;?}

????????????//剪辑位图并保存???????????

????????????for?(int?y?=?1;?y?=?iY;?y++)

????????????{

????????????????for?(int?x?=?1;?x?=?iX;?x++)

????????????????{

????????????????????int?i?=?x?+?(y?-?1)?*?iX;

????????????????????Rectangle?源矩形?=?new?Rectangle(

????????????????????????(x?-?1)?*?W?-?OX,?(y?-?1)?*?H?-?OY,?W,?H);

????????????????????Rectangle?目标矩形?=?new?Rectangle(0,?0,?W,?H);

????????????????????Bitmap?t剪辑?=?new?Bitmap(W,?H,?PixelFormat.Format32bppArgb);

????????????????????Graphics?g?=?Graphics.FromImage(t剪辑);

????????????????????g.DrawImage(当前,?目标矩形,?源矩形,?GraphicsUnit.Pixel);

????????????????????g.Dispose();

????????????????????string?路径?=?Path.Combine(输出目录,?获取文件名(i)?+?获取后缀名());

????????????????????t剪辑.Save(路径,?图像格式);

????????????????}

????????????}

????????}

????????private?string?获取文件名(int?ID)

????????{

????????????string?文件名?=?System.IO.Path.GetFileNameWithoutExtension(文件地址);

????????????string?strID?=?ID.ToString();

????????????string?strDate?=?DateTime.Now.ToString("yyyyMMdd");

????????????return?string.Format(命名格式,?文件名,?strID,?strDate);

????????}

????????private?string?获取后缀名()

????????{

????????????return?输出后缀名;

????????}

????}

}

调用方法

精灵.打开(文件地址);

精灵.W?=?32;?//每个分割方块的宽度

精灵.H?=?32;?//每个分割方块的高度

//精灵.透明色?=?Color.FromArgb(255,255,255);?//需要透明色就删掉注释

//精灵.开关透明?=?true;

精灵.输出目录?=?@"D:\Out";

精灵.裁剪();

C#程序设计窗体如何将导入的图片背景变成透明?

c#好像只支持png格式的透明。bmp和jpg不支持透明。

图片透明方法:

bitmap2 = new Bitmap(文件路径或Image对象);

bitmap2.MakeTransparent(Color.White);

如果你要做游戏的话,多个picturebox是会互相覆盖的(即使图片透明)。

所以要使用GDI绘图的方法,才能透明。

Graphics1 = Form1.CreateGraphics();

Graphics1.DrawImage(位图, X, Y, 宽, 高);

C#怎么去了图片背景色并且可以调亮度

您好,软糖来回答把,我做过一个去图片背景的程序。

核心实现就3句话,主要使用Bitmap.MakeTransparent方法

Bitmap?bitmap2?=?new?Bitmap(pictureBox1.Image);

bitmap2.MakeTransparent(背景色);

pictureBox2.Image?=?bitmap2;

如果想要调整亮度,就要应用到GDI的图像特性ImageAttributes了

我这里有封装好的图像特性:

using?System;

using?System.Drawing;

using?System.Drawing.Drawing2D;

using?System.Drawing.Imaging;

namespace?引擎.绘图.GDI

{

????public?class?图像特性?:?IDisposable?{

????????public?图像特性()?{

????????????M特性?=?new?ImageAttributes();

????????????M矩阵?=?原始矩阵();

????????}

????????public?图像特性(float?透明度)?{

????????????M特性?=?new?ImageAttributes();

????????????M矩阵?=?原始矩阵();

????????????this.透明度?=?透明度;

????????}

????????public?图像特性(float?透明度,?float?gamma)?{

????????????M特性?=?new?ImageAttributes();

????????????M矩阵?=?原始矩阵();????????

????????????this.透明度?=?透明度;

????????????M特性.SetGamma(gamma,?ColorAdjustType.Bitmap);

????????}

????????ImageAttributes?M特性;

????????ColorMatrix?M矩阵;

????????float?M亮度?=?1.0f;

????????bool?M灰化?=?false;

????????public?ColorMatrixFlag?灰色底纹?=?ColorMatrixFlag.Default;

????????public?ColorAdjustType?色彩校正模式?=?ColorAdjustType.Default;

????????public?ImageAttributes?资源?{?get?{?return?获取资源();?}?}

????????protected?virtual?ImageAttributes?获取资源()?{?return?M特性;?}

????????protected?virtual?void?设置资源(ImageAttributes?v特性)?{?M特性?=?v特性;?}

????????public?ColorMatrix?矩阵?{?get?{?return?M矩阵;?}?}?

????????public?void?透明色?(?Color?颜色?)?{?M特性.SetColorKey(颜色,?颜色);?}

????????public?void?设置gamma?(?float?gamma?)?{?M特性.SetGamma(gamma,?ColorAdjustType.Bitmap);?}

????????public?void?清除色彩校正?()?{?M特性.SetNoOp(色彩校正模式);?}

????????public?bool?灰化?{?get?{?return?M灰化;?}?set?{?M灰化?=?value;?灰度化(value);?}?}

????????public?float?透明度?{?get?{?return?M矩阵[3,?3];?}?set?{?M矩阵[3,?3]?=?value;?应用矩阵();?}?}

????????public?float?R浓度?{?get?{?return?M矩阵[0,?0];?}?set?{?M矩阵[0,?0]?=?value;?应用矩阵();?}?}

????????public?float?G浓度?{?get?{?return?M矩阵[1,?1];?}?set?{?M矩阵[1,?1]?=?value;?应用矩阵();?}?}

????????public?float?B浓度?{?get?{?return?M矩阵[2,?2];?}?set?{?M矩阵[2,?2]?=?value;?应用矩阵();?}?}

????????public?void?设置浓度?(?Color?C?)

????????{

????????????float?r?=?(float)C.R?/?255f;

????????????float?g?=?(float)C.G?/?255f;

????????????float?b?=?(float)C.B?/?255f;

????????????float?a?=?(float)C.A?/?255f;

????????????M矩阵[0,?0]?=?r;

????????????M矩阵[1,?1]?=?g;

????????????M矩阵[2,?2]?=?b;

????????????M矩阵[3,?3]?=?a;

????????????应用矩阵();

????????}

????????public?void?设置浓度?(?float?R,?float?G,?float?B)

????????{

????????????M矩阵[0,?0]?=?R;

????????????M矩阵[1,?1]?=?G;

????????????M矩阵[2,?2]?=?B;

????????????应用矩阵();

????????}

????????public?float?亮度

????????{

????????????get?{?return?M亮度;?}?

????????????set?

????????????{

????????????????M亮度?=?value;

????????????????M矩阵[4,?0]?=?M亮度;

????????????????M矩阵[4,?1]?=?M亮度;

????????????????M矩阵[4,?2]?=?M亮度;

????????????????应用矩阵();?

????????????}?

????????}

????????public?ColorMatrix?原始矩阵?(?)

????????{?

????????????????float[][]?t颜色矩阵元素?=?{?

????????????????new?float[]?{1,??0,??0,??0,??0},????????//?red?scaling?factor?of?2

????????????????new?float[]?{0,??1,??0,??0,??0},????????//?green?scaling?factor?of?1

????????????????new?float[]?{0,??0,??1,??0,??0},????????//?blue?scaling?factor?of?1

????????????????new?float[]?{0,??0,??0,??1,??0},????????//?alpha?scaling?factor?of?1

????????????????new?float[]?{0,??0,??0,??0,??1}};

????????????return?new?ColorMatrix(t颜色矩阵元素);??????

????????}

????????public?void?应用矩阵?(?)??{?

????????????if?(M矩阵?==?null)?{?M矩阵?=?原始矩阵();?};

????????????M特性.SetColorMatrix(M矩阵);

????????}

????????public?void?应用矩阵(Color?C)?{

????????????float[][]?t颜色矩阵元素?=?{

????????????????new?float[]?{?C.R?/?255f,??0,??0,??0,??0},????

????????????????new?float[]?{0,?C.G?/?255f,??0,??0,??0},?????

????????????????new?float[]?{0,??0,?C.B?/?255f,??0,??0},??????

????????????????new?float[]?{0,??0,??0,?C.A?/?255f,??0},????

????????????????new?float[]?{0,??0,??0,??0,??1}};

????????????M矩阵?=??new?ColorMatrix(t颜色矩阵元素);

????????????M特性.SetColorMatrix(M矩阵);

????????}

????????public?void?清空矩阵?(?)?{?M特性.ClearColorMatrix();?}

????????protected?void?灰度化(bool?应用)

????????{

????????????if?(应用?==?true)

????????????{

????????????????M矩阵[0,?0]?=?0.3f;?M矩阵[0,?1]?=?0.3f;?M矩阵[0,?2]?=?0.3f;

????????????????M矩阵[1,?0]?=?0.59f;?M矩阵[1,?1]?=?0.59f;?M矩阵[1,?2]?=?0.59f;

????????????????M矩阵[2,?0]?=?0.11f;?M矩阵[2,?1]?=?0.11f;?M矩阵[2,?2]?=?0.11f;

????????????????应用矩阵();

????????????}

????????????else?

????????????{

????????????????float?A?=?M矩阵[3,?3];

????????????????float?L0?=?M矩阵[4,?0];?float?L1?=?M矩阵[4,?1];?float?L2?=?M矩阵[4,?2];

????????????????M矩阵?=?原始矩阵();

????????????????M矩阵[3,?3]=A;M矩阵[4,?0]=L0;M矩阵[4,?1]=L1;M矩阵[4,?2]=L2;

????????????????应用矩阵();

????????????}

????????}

????????public?void?Dispose()

????????{

????????????M特性.Dispose();

????????}

????}

}

用法

首先new一个图像特性

然后图像特性.亮度= 0.1f ~ 10.0f (设置float表示十分之一或十倍亮度)

最后调用绘制图像()方法,把图像特性填进去。

//画纸是Graphics

public?GraphicsUnit?绘图单位?=?GraphicsUnit.Pixel;

public?void?绘制图像(Bitmap?图像,?int?X,?int?Y,?int?宽,?int?高,?图像特性?特性)

{?画纸.DrawImage(位图,?new?Rectangle(X,?Y,?宽,?高),?

0,?0,?图像.Width,?图像.Height,?绘图单位,?特性.资源);?}

满意请采纳,谢谢。

C# 怎样设置bitmap格式图片的透明度

C#可以使用Bitmap.MakeTransparent 方法 (Color),使指定的颜色对此?Bitmap?透明。

参考代码如下:

public?void?MakeTransparent(

Color?transparentColor

)

参数介绍如下:

名称:transparentColor

类型:System.Drawing.Color

Color?结构,它表示要使之透明的颜色。

命名空间:??System.Drawing

程序集:??System.Drawing(在 System.Drawing.dll 中)

(责任编辑:IT教学网)

更多

推荐Flash actionscript文章