Problem with converting from bitmap to ImageBitmap in c#











up vote
0
down vote

favorite
1












When I convert two pixels bitmap to imagebitmap I change it to common image with many pixcels and chenging the to the gradiend (black and white pixel are now gradient from black throught grey to white)



public static BitmapImage Bitmap2BitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

return bitmapImage;
}
}









share|improve this question
























  • So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
    – Klaus Gütter
    Nov 10 at 7:12










  • I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
    – Oskar Zadora
    Nov 10 at 10:35










  • Maybe this will help you: stackoverflow.com/questions/2381012/…
    – Klaus Gütter
    Nov 10 at 11:02










  • You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
    – Hans Passant
    Nov 10 at 14:10










  • So how to turn off the SmoothingMode in BitmapImage?
    – Oskar Zadora
    Nov 10 at 21:40















up vote
0
down vote

favorite
1












When I convert two pixels bitmap to imagebitmap I change it to common image with many pixcels and chenging the to the gradiend (black and white pixel are now gradient from black throught grey to white)



public static BitmapImage Bitmap2BitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

return bitmapImage;
}
}









share|improve this question
























  • So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
    – Klaus Gütter
    Nov 10 at 7:12










  • I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
    – Oskar Zadora
    Nov 10 at 10:35










  • Maybe this will help you: stackoverflow.com/questions/2381012/…
    – Klaus Gütter
    Nov 10 at 11:02










  • You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
    – Hans Passant
    Nov 10 at 14:10










  • So how to turn off the SmoothingMode in BitmapImage?
    – Oskar Zadora
    Nov 10 at 21:40













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





When I convert two pixels bitmap to imagebitmap I change it to common image with many pixcels and chenging the to the gradiend (black and white pixel are now gradient from black throught grey to white)



public static BitmapImage Bitmap2BitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

return bitmapImage;
}
}









share|improve this question















When I convert two pixels bitmap to imagebitmap I change it to common image with many pixcels and chenging the to the gradiend (black and white pixel are now gradient from black throught grey to white)



public static BitmapImage Bitmap2BitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

return bitmapImage;
}
}






c# image






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 7:06









Foo

1




1










asked Nov 10 at 6:50









Oskar Zadora

1




1












  • So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
    – Klaus Gütter
    Nov 10 at 7:12










  • I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
    – Oskar Zadora
    Nov 10 at 10:35










  • Maybe this will help you: stackoverflow.com/questions/2381012/…
    – Klaus Gütter
    Nov 10 at 11:02










  • You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
    – Hans Passant
    Nov 10 at 14:10










  • So how to turn off the SmoothingMode in BitmapImage?
    – Oskar Zadora
    Nov 10 at 21:40


















  • So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
    – Klaus Gütter
    Nov 10 at 7:12










  • I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
    – Oskar Zadora
    Nov 10 at 10:35










  • Maybe this will help you: stackoverflow.com/questions/2381012/…
    – Klaus Gütter
    Nov 10 at 11:02










  • You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
    – Hans Passant
    Nov 10 at 14:10










  • So how to turn off the SmoothingMode in BitmapImage?
    – Oskar Zadora
    Nov 10 at 21:40
















So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
– Klaus Gütter
Nov 10 at 7:12




So what is your problem? Do you want the generated BitmapImage to also be 2 pixels wide? If yes, you can set the DecodePixelWidth/DecodePixelHeight properties on bitmapImage.
– Klaus Gütter
Nov 10 at 7:12












I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
– Oskar Zadora
Nov 10 at 10:35




I don't need 2 pixels image, when i'm creating two pixels bitmap, one pixel black second white, then i'm converting it to imagebitmap and i want to get image half black half white, but i get color gradient from black to white (you know the border is not clear ,the color changing from one to another smoothly)
– Oskar Zadora
Nov 10 at 10:35












Maybe this will help you: stackoverflow.com/questions/2381012/…
– Klaus Gütter
Nov 10 at 11:02




Maybe this will help you: stackoverflow.com/questions/2381012/…
– Klaus Gütter
Nov 10 at 11:02












You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
– Hans Passant
Nov 10 at 14:10




You are focusing on the wrong problem. Go back to the code that generated the Bitmap object. The Graphics.InterpolationMode and Graphics.PixelOffsetMode properties that were used to generate it are a very big deal on very small bitmaps that are blown up to larger sizes. Perhaps Graphics.SmoothingMode, hard to tell with nothing to look at.
– Hans Passant
Nov 10 at 14:10












So how to turn off the SmoothingMode in BitmapImage?
– Oskar Zadora
Nov 10 at 21:40




So how to turn off the SmoothingMode in BitmapImage?
– Oskar Zadora
Nov 10 at 21:40












2 Answers
2






active

oldest

votes

















up vote
0
down vote













public Bitmap getRandomTwo()
{
Bitmap alone = new Bitmap(1, 2);
alone.SetPixel(0, 0, Color.White);
alone.SetPixel(0, 1,Color.Black);
return alone;
}


this is the code to generate bitmap






share|improve this answer




























    up vote
    0
    down vote













    Update:
    Here is a method which can scale a Bitmap without smoothing.



            public static Bitmap Scale(int count, Bitmap source)
    {
    if (count <= 0) { return source; }
    var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
    var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    unsafe
    {
    var srcByte = (byte*)sourcedata.Scan0.ToPointer();
    var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
    for (var y = 0; y < bitmapdata.Height; y++) {
    for (var x = 0; x < bitmapdata.Width; x++) {
    long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
    dstByte[0] = srcByte[index];
    dstByte[1] = srcByte[index + 1];
    dstByte[2] = srcByte[index + 2];
    dstByte[3] = srcByte[index + 3];
    dstByte += 4;
    }
    }
    }
    source.UnlockBits(sourcedata);
    bitmap.UnlockBits(bitmapdata);

    return bitmap;
    }


    In this situation, you can get the correct BitmapSource with:



       var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
    .ToBitmapSource();//Convert to bitmap source




    if you just want to covert a Bitmap into BitmapSource to show in WPF Controls, try this method:



        [DllImport("gdi32", EntryPoint = "DeleteObject")]
    public static extern bool DeleteHBitmap(IntPtr hObject);


    public static BitmapSource ToBitmapSource(this Bitmap target)
    {
    var hbitmap = target.GetHbitmap();
    try {
    return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
    BitmapSizeOptions.FromEmptyOptions());
    }
    finally {
    DeleteHBitmap(hbitmap);
    }
    }


    Don't ignore the DeleteHBitmap call!






    share|improve this answer























    • Not working, this same effect
      – Oskar Zadora
      Nov 10 at 14:28










    • Updated, you must scale the bitmap before converting it to BitmapSource
      – Saber0905
      Nov 11 at 1:20










    • Thx very much !!!!!!!!! Super
      – Oskar Zadora
      Nov 11 at 8:15











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236682%2fproblem-with-converting-from-bitmap-to-imagebitmap-in-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    public Bitmap getRandomTwo()
    {
    Bitmap alone = new Bitmap(1, 2);
    alone.SetPixel(0, 0, Color.White);
    alone.SetPixel(0, 1,Color.Black);
    return alone;
    }


    this is the code to generate bitmap






    share|improve this answer

























      up vote
      0
      down vote













      public Bitmap getRandomTwo()
      {
      Bitmap alone = new Bitmap(1, 2);
      alone.SetPixel(0, 0, Color.White);
      alone.SetPixel(0, 1,Color.Black);
      return alone;
      }


      this is the code to generate bitmap






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        public Bitmap getRandomTwo()
        {
        Bitmap alone = new Bitmap(1, 2);
        alone.SetPixel(0, 0, Color.White);
        alone.SetPixel(0, 1,Color.Black);
        return alone;
        }


        this is the code to generate bitmap






        share|improve this answer












        public Bitmap getRandomTwo()
        {
        Bitmap alone = new Bitmap(1, 2);
        alone.SetPixel(0, 0, Color.White);
        alone.SetPixel(0, 1,Color.Black);
        return alone;
        }


        this is the code to generate bitmap







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 14:34









        Oskar Zadora

        1




        1
























            up vote
            0
            down vote













            Update:
            Here is a method which can scale a Bitmap without smoothing.



                    public static Bitmap Scale(int count, Bitmap source)
            {
            if (count <= 0) { return source; }
            var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
            var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
            var srcByte = (byte*)sourcedata.Scan0.ToPointer();
            var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
            for (var y = 0; y < bitmapdata.Height; y++) {
            for (var x = 0; x < bitmapdata.Width; x++) {
            long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
            dstByte[0] = srcByte[index];
            dstByte[1] = srcByte[index + 1];
            dstByte[2] = srcByte[index + 2];
            dstByte[3] = srcByte[index + 3];
            dstByte += 4;
            }
            }
            }
            source.UnlockBits(sourcedata);
            bitmap.UnlockBits(bitmapdata);

            return bitmap;
            }


            In this situation, you can get the correct BitmapSource with:



               var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
            .ToBitmapSource();//Convert to bitmap source




            if you just want to covert a Bitmap into BitmapSource to show in WPF Controls, try this method:



                [DllImport("gdi32", EntryPoint = "DeleteObject")]
            public static extern bool DeleteHBitmap(IntPtr hObject);


            public static BitmapSource ToBitmapSource(this Bitmap target)
            {
            var hbitmap = target.GetHbitmap();
            try {
            return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
            DeleteHBitmap(hbitmap);
            }
            }


            Don't ignore the DeleteHBitmap call!






            share|improve this answer























            • Not working, this same effect
              – Oskar Zadora
              Nov 10 at 14:28










            • Updated, you must scale the bitmap before converting it to BitmapSource
              – Saber0905
              Nov 11 at 1:20










            • Thx very much !!!!!!!!! Super
              – Oskar Zadora
              Nov 11 at 8:15















            up vote
            0
            down vote













            Update:
            Here is a method which can scale a Bitmap without smoothing.



                    public static Bitmap Scale(int count, Bitmap source)
            {
            if (count <= 0) { return source; }
            var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
            var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
            var srcByte = (byte*)sourcedata.Scan0.ToPointer();
            var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
            for (var y = 0; y < bitmapdata.Height; y++) {
            for (var x = 0; x < bitmapdata.Width; x++) {
            long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
            dstByte[0] = srcByte[index];
            dstByte[1] = srcByte[index + 1];
            dstByte[2] = srcByte[index + 2];
            dstByte[3] = srcByte[index + 3];
            dstByte += 4;
            }
            }
            }
            source.UnlockBits(sourcedata);
            bitmap.UnlockBits(bitmapdata);

            return bitmap;
            }


            In this situation, you can get the correct BitmapSource with:



               var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
            .ToBitmapSource();//Convert to bitmap source




            if you just want to covert a Bitmap into BitmapSource to show in WPF Controls, try this method:



                [DllImport("gdi32", EntryPoint = "DeleteObject")]
            public static extern bool DeleteHBitmap(IntPtr hObject);


            public static BitmapSource ToBitmapSource(this Bitmap target)
            {
            var hbitmap = target.GetHbitmap();
            try {
            return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
            DeleteHBitmap(hbitmap);
            }
            }


            Don't ignore the DeleteHBitmap call!






            share|improve this answer























            • Not working, this same effect
              – Oskar Zadora
              Nov 10 at 14:28










            • Updated, you must scale the bitmap before converting it to BitmapSource
              – Saber0905
              Nov 11 at 1:20










            • Thx very much !!!!!!!!! Super
              – Oskar Zadora
              Nov 11 at 8:15













            up vote
            0
            down vote










            up vote
            0
            down vote









            Update:
            Here is a method which can scale a Bitmap without smoothing.



                    public static Bitmap Scale(int count, Bitmap source)
            {
            if (count <= 0) { return source; }
            var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
            var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
            var srcByte = (byte*)sourcedata.Scan0.ToPointer();
            var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
            for (var y = 0; y < bitmapdata.Height; y++) {
            for (var x = 0; x < bitmapdata.Width; x++) {
            long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
            dstByte[0] = srcByte[index];
            dstByte[1] = srcByte[index + 1];
            dstByte[2] = srcByte[index + 2];
            dstByte[3] = srcByte[index + 3];
            dstByte += 4;
            }
            }
            }
            source.UnlockBits(sourcedata);
            bitmap.UnlockBits(bitmapdata);

            return bitmap;
            }


            In this situation, you can get the correct BitmapSource with:



               var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
            .ToBitmapSource();//Convert to bitmap source




            if you just want to covert a Bitmap into BitmapSource to show in WPF Controls, try this method:



                [DllImport("gdi32", EntryPoint = "DeleteObject")]
            public static extern bool DeleteHBitmap(IntPtr hObject);


            public static BitmapSource ToBitmapSource(this Bitmap target)
            {
            var hbitmap = target.GetHbitmap();
            try {
            return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
            DeleteHBitmap(hbitmap);
            }
            }


            Don't ignore the DeleteHBitmap call!






            share|improve this answer














            Update:
            Here is a method which can scale a Bitmap without smoothing.



                    public static Bitmap Scale(int count, Bitmap source)
            {
            if (count <= 0) { return source; }
            var bitmap = new Bitmap(source.Size.Width * count, source.Size.Height * count);
            var sourcedata = source.LockBits(new Rectangle(new System.Drawing.Point(0, 0), source.Size), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var bitmapdata = bitmap.LockBits(new Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
            var srcByte = (byte*)sourcedata.Scan0.ToPointer();
            var dstByte = (byte*)bitmapdata.Scan0.ToPointer();
            for (var y = 0; y < bitmapdata.Height; y++) {
            for (var x = 0; x < bitmapdata.Width; x++) {
            long index = (x / count) * 4 + (y / count) * sourcedata.Stride;
            dstByte[0] = srcByte[index];
            dstByte[1] = srcByte[index + 1];
            dstByte[2] = srcByte[index + 2];
            dstByte[3] = srcByte[index + 3];
            dstByte += 4;
            }
            }
            }
            source.UnlockBits(sourcedata);
            bitmap.UnlockBits(bitmapdata);

            return bitmap;
            }


            In this situation, you can get the correct BitmapSource with:



               var bitmapSrc = MyImagingHelper.Scale(100,twoPixelBitmap)//this will get a Bitmap size 200 * 100,change the ratio if it is not enough
            .ToBitmapSource();//Convert to bitmap source




            if you just want to covert a Bitmap into BitmapSource to show in WPF Controls, try this method:



                [DllImport("gdi32", EntryPoint = "DeleteObject")]
            public static extern bool DeleteHBitmap(IntPtr hObject);


            public static BitmapSource ToBitmapSource(this Bitmap target)
            {
            var hbitmap = target.GetHbitmap();
            try {
            return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
            DeleteHBitmap(hbitmap);
            }
            }


            Don't ignore the DeleteHBitmap call!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 1:19

























            answered Nov 10 at 13:31









            Saber0905

            11




            11












            • Not working, this same effect
              – Oskar Zadora
              Nov 10 at 14:28










            • Updated, you must scale the bitmap before converting it to BitmapSource
              – Saber0905
              Nov 11 at 1:20










            • Thx very much !!!!!!!!! Super
              – Oskar Zadora
              Nov 11 at 8:15


















            • Not working, this same effect
              – Oskar Zadora
              Nov 10 at 14:28










            • Updated, you must scale the bitmap before converting it to BitmapSource
              – Saber0905
              Nov 11 at 1:20










            • Thx very much !!!!!!!!! Super
              – Oskar Zadora
              Nov 11 at 8:15
















            Not working, this same effect
            – Oskar Zadora
            Nov 10 at 14:28




            Not working, this same effect
            – Oskar Zadora
            Nov 10 at 14:28












            Updated, you must scale the bitmap before converting it to BitmapSource
            – Saber0905
            Nov 11 at 1:20




            Updated, you must scale the bitmap before converting it to BitmapSource
            – Saber0905
            Nov 11 at 1:20












            Thx very much !!!!!!!!! Super
            – Oskar Zadora
            Nov 11 at 8:15




            Thx very much !!!!!!!!! Super
            – Oskar Zadora
            Nov 11 at 8:15


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236682%2fproblem-with-converting-from-bitmap-to-imagebitmap-in-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings