C# 图像处理亮度
我想更改图像的亮度值。YouTube上也有很多这样的视频,但视频使用的是c#中的picturebox。我想用wpf中的图像制作。但是wpf没有对image.source使用位图。需要位图图像。我试了些东西,但没用 亮度代码:C# 图像处理亮度,c#,wpf,bitmap,C#,Wpf,Bitmap,我想更改图像的亮度值。YouTube上也有很多这样的视频,但视频使用的是c#中的picturebox。我想用wpf中的图像制作。但是wpf没有对image.source使用位图。需要位图图像。我试了些东西,但没用 亮度代码: public static Bitmap AdjustBrightness(Bitmap Image,int Value) { Bitmap TempBitmap = Image; float FinalValue =
public static Bitmap AdjustBrightness(Bitmap Image,int Value)
{
Bitmap TempBitmap = Image;
float FinalValue = (float)Value / 255.0f;
Bitmap NewBitmap = new Bitmap(TempBitmap.Width,TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix =
{
new float[] {1,0,0,0,0},
new float[] {0,1,0,0,0},
new float[] {0,0,1,0,0},
new float[] {0,0,0,1,0},
new float[] {FinalValue,FinalValue,FinalValue,1,1},
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;
}
如果我尝试返回“无法将类型“System.Drawing.Bitmap”隐式转换为“System.Windows.Media.ImageSource”:
因此,我使用位图创建图像源:
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
现在我尝试这样的imagesource:
img_goruntucontrol.Source = BitmapToImageSource(goruntu.AdjustBrightness(img_bitmap, Int32.Parse(slider_brig.Value.ToString())));
我的位图代码:bitmap img\u bitmap=新位图(图像\u源)代码>图像\源是一个字符串值
我尝试为img_位图运行return“不支持URI格式”
图像\u源值:“file:///C:/Users/kamilkunt/Documents/Dentatech/goruntuler/9/3512.jpg“
为此,您应该在WPF中使用WriteableBitmap或InteropBitmap。请参见此处:我在调整亮度函数时将位图更改为WriteableBitmap,而不需要将位图映射为图像源函数。但我如何修复img_位图uri错误?@Clemens我尝试修复,但返回了一个错误:
img_goruntucontrol.Source = BitmapToImageSource(goruntu.AdjustBrightness(img_bitmap, Int32.Parse(slider_brig.Value.ToString())));