2

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"

Il Vic
  • 5,576
  • 4
  • 26
  • 37
  • You should use WriteableBitmap or InteropBitmap for this purpose in WPF. – Pavel Mar 02 '18 at 09:54
  • See here: https://stackoverflow.com/q/26672406/1136211 – Clemens Mar 02 '18 at 09:56
  • I changed the Bitmap to WriteableBitmap on AdjustBrightness function and I dont need to BitmapToImageSource function. But how can I fix img_bitmap uri error? –  Mar 02 '18 at 10:01
  • @Clemens I try to that but return a error: https://ibb.co/fbneYH –  Mar 02 '18 at 10:14

0 Answers0