【发布时间】:2015-11-18 15:24:57
【问题描述】:
我写了一个函数来改变图像的 alpha 系数。我使用 setpixel 和 getpixel,这非常慢。我发现 Lockbits 方法更快。我怎样才能用 Lockbits 做到这一点? 这是我当前的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private static Image Tran(Image s,int alpha)
{
int x = 0, y = 0;
Bitmap tImage = new Bitmap(s);
for (x = 0; x < tImage.Width; x++)
{
for (y = 0; y < tImage.Height; y++)
{
tImage.SetPixel(x, y, Color.FromArgb(alpha, tImage.GetPixel(x, y).R, tImage.GetPixel(x, y).G, tImage.GetPixel(x, y).B));
}
}
return tImage;
}
public Form1()
{
InitializeComponent();
trackBar1.TickStyle = TickStyle.Both;
trackBar1.Orientation = Orientation.Vertical;
trackBar1.Minimum = 0;
trackBar1.Maximum = 255;
trackBar1.Height = 101;
trackBar1.Value = 255;
pictureBox1.Image = Image.FromFile("C:\\Users\\rati\\Desktop\\water.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
pictureBox1.Image = ChangeAlpha(pictureBox1.Image, trackBar1.Value);
textBox1.Text = trackBar1.Value.ToString();
}
}
}
【问题讨论】:
-
this post 中的第一个例程可能会作为 Lockbits 示例感兴趣..
-
代码中的主要问题是当您更改不透明度时,您再次将结果设置为图片框的图像并再次在结果上应用不透明度。换句话说,您对之前应用不透明度的图像应用不透明度。
-
Reza,你还有什么建议吗?
-
查看我的答案的编辑部分,找到您在使用该方法时遇到的问题:)
标签: c# winforms pixel alpha lockbits