c是子窗体,currentSelectedTemplate.BackgroundPic是背景图片路径 绘制完背景,c显示一下就可以了c.Show();
由于SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.FromArgb(50, 50, 50, 50);
只能用在父窗体是继承自Form的情况下,所以在子窗体下面绘制一个picbox是不现实的,本来子窗体设置了透明,其与父窗体中间的夹层被picbox遮挡也是没有效果的,因为子窗体只能穿越form透明
所以可以给父窗体设置背景,然后用子窗体透明显示 这样就是子窗体相当于父窗体透明.
但是from的背景设置只能支持其中的几种选项,比如居中拉伸平铺这样的基本渲染方式.
我们通过SetLogo,把一张图的其他部分都设置成透明的,用于父窗口当背景,只有子窗体部分区域绘制我们需要的背景图.
这其中的一些方法很适用于洁面开发 图像合成等.
如果不使用此方法 使用类似双缓冲之类的,一样还是会让子窗体很卡
<pre name="code" class="csharp">public static Image GetNewImage2(Image oldImgPath, int newWidth, int newHeight)
{
Image oldImg = Image.FromFile(oldImgPath); // 加载原图片
//Bitmap oldImage = oldImgPath.Clone() as Bitmap;
//Image newImg = oldImgPath.GetThumbnailImage(newWidth, newHeight, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero);
对原图片进行缩放 12.
//return newImg;
Bitmap curBitmap = new Bitmap(oldImgPath);
int width = curBitmap.Width;
int height = curBitmap.Height;
int length = height * 3 * width;
byte[] RGB = new byte[length];
if (curBitmap != null)
{
BitmapData data = curBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
System.IntPtr Scan0 = data.Scan0;
int stride = data.Stride;
System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length);
unsafe
{
//byte b = 0;
byte* p = (byte*)Scan0;
int offset = stride - width * 3;
//double gray = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//gray = 0.3 * p[2] + 0.59 * p[1] + 0.11 * p[0];
//gray = 0.1 * p[2] + 1 * p[1] + 1 * p[0];
//b = (byte)gray;
//b = 0;
//if (b == 255)
//{
// b = 0;
//}
//if (p[2] == 255 && p[1] == 255 && p[0] == 255)
//{
// //p[2] = p[1] = p[0] = 0;
//}
p += 3;
}
p += offset;
}
}
curBitmap.UnlockBits(data);
}
//return curBitmap;
return curBitmap.GetThumbnailImage(newWidth, newHeight, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero);
}
</pre><pre name="code" class="csharp">
</pre><pre name="code" class="csharp">
<pre name="code" class="csharp"> /// <summary>
/// 合并图片,支持透明度选项
/// </summary>
/// <param name="b0">原始图</param>
/// <param name="b1">LOGO图</param>
/// <param name="val">透明度,取值[0,1],数值越大LOGO越不透明</param>
/// <param name="x">LOGO在原始图上的起始位置X</param>
/// <param name="y">LOGO在原始图上的起始位置Y</param>
/// <returns>完成后的贴图</returns>
public static Bitmap SetLogo(Bitmap b0, Bitmap b1, float val, int x, int y)
{
if (b0 == null) return null;
if (b1 == null) return b0;
Graphics g = null;
try
{
g = Graphics.FromImage(b0);
float[][] data =
{
new float[] {1f,0f,0f,0f,0f},
new float[] {0f,1f,0f,0f,0f},
new float[] {0f,0f,1f,0f,0f},
new float[] {0f,0f,0f,val,0f},
new float[] {0f,0f,0f,0f,1f}
};
ColorMatrix mx = new ColorMatrix(data);
ImageAttributes imgatt = new ImageAttributes();
imgatt.SetColorMatrix(mx, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(b1, new Rectangle(x, y, b1.Width, b1.Height), 0, 0, b1.Width, b1.Height, GraphicsUnit.Pixel, imgatt);
return b0;
}
catch
{
return null;
}
finally
{
if (g != null) g.Dispose();
}
}
整体看起来是跟子窗体设置背景一样的效果,而且绝对不会卡顿延迟 不懂跟帖