I want to change an image's brightness value. YouTube also has a lot of videos for that but videos used picturebox in c#. I want to make with image in wpf. But wpf didnt use bitmap for image.source. Need to BitmapImage. I try something but didnt work.
Brightness Code:
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;
}
if I try to that return "Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Windows.Media.ImageSource'" :
img_goruntucontrol.Source = goruntu.AdjustBrightness(img_bitmap, Int32.Parse(slider_brig.Value.ToString()));
So I using bitmap to 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;
}
}
Now I try imagesource like that:
img_goruntucontrol.Source = BitmapToImageSource(goruntu.AdjustBrightness(img_bitmap, Int32.Parse(slider_brig.Value.ToString())));
my bitmap code:Bitmap img_bitmap = new Bitmap(image_source);
image_source is a string value.
I try to run return "URI formats are not supported." for img_bitmap.
image_source value: "file:///C:/Users/kamilkunt/Documents/Dentatech/goruntuler/9/3512.jpg"