WritePreamble() before FormatConvertedBitmap?

Nov 2, 2011 at 3:56 AM
WritePreamble();
 _formatConverterSourceFormat = sourceFormat;
 _formatConverter = new FormatConvertedBitmap(source, destinationFormat, Palette, 0);
 WritePostscript();
In some sample code, WritePreamble() and  WritePostscript() are used before converting bitmapformat.
but in msdn sample code no WritePreamble() and WritePostscript().
Should I use those?
What is the good point to use those two functions?

Help me.
Thank you
Coordinator
Nov 2, 2011 at 9:06 PM

WritePreamble is ensures that the freezable can be changed.  It checkes two conditions:

1) That the calling thread is the owner thread of the freezable.  Freezables are not thread-safe, they can only be updated by the thread that created them.
2) That the freezable is not frozen.  Frozen freezables cannot be modified, that are in a read-only state.

WritePostscript raises the changed notifications for the freezable.  This includes calling the OnChanged virtual, invalidating sub properties, and raising the Changed event.

 

Both are extrememly important to the correct functioning of a freezable.

Nov 3, 2011 at 1:39 AM
Edited Nov 3, 2011 at 1:44 AM

Thank you for your answer.

Let me take an example.

- MainWindow.xaml

<Grid>
  <Image x:Name="img" RenderOptions.BitmapScalingMode="NearestNeighbor" />
  <TextBlock x:Name="text" Text="0001"/>
</Grid>

- MainWindow.cs

   There is Timer and in Timer function TextBlock updates its content per two seconds and Image updates its content per ten seconds like following.

  if(_nTime % 10 == 0)//. per 10 seconds update Image
  {
     BitmapSource bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                _hBitmapStaff,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

     ColorKeyBitmap colorKeyBmp = new ColorKeyBitmap();
     colorKeyBmp.TransparentColor = Color.FromArgb(255, 255, 255, 255);
     colorKeyBmp.Source = bitSrc;
     img.Source = colorKeyBmp;
  }

  if(_nTime % 2 == 0)//. per 2 seconds update TextBlock
  {
      text.Text = String.Format("{0}", _nTime);
      //text.invalidateVisible();
      //this.invalidateVisible();
  }
But as I mentioned before TextBlock only updates when Image is udated.
So I customize ChainedBitmap and remove WritePostscript then TextBlock is updated per 2 seconds.
I don't think the main reason is WritePostscript but I tried everything I can do.
I want you to do the code above in your spare time and help me what's the reason.
Thanks again.
Coordinator
Nov 3, 2011 at 2:08 AM

I started to try to make a repro, then realized that it was probably a better idea for you to provide the repro.  That way we know we are talking about the right issues.  Try to make the repro as focused as possible.

Nov 3, 2011 at 2:39 AM

That's a good idea. Thank you.

But I am not as good as you to do the good job.

Now I really want you to give me a hint where I should update.

Help me.

Coordinator
Nov 3, 2011 at 2:44 AM

You obviously have some code already, which is where you see this problem.  Can't you share it with me?

Nov 3, 2011 at 3:04 AM

First, Thank you very much for you production.

I will do my best to contribute to this.

... ... ... ... ...

What I did for my problem is only removing WritePostscript() and Nothing else.

Following Code shows what I did.

in ChainedBitmap.cs

        protected override void CopyPixelsCore(Int32Rect sourceRect, int stride, int bufferSize, IntPtr buffer)
        {
            BitmapSource source = Source;
            BitmapSource convertedSource = source;

            if (source != null)
            {
                PixelFormat sourceFormat = source.Format;
                PixelFormat destinationFormat = Format;

                if (sourceFormat != destinationFormat)
                {
                    // We need a format converter.  Reuse the cached one if
                    // it still matches.
                    if (_formatConverter == null ||
                        _formatConverter.Source != source ||
                        _formatConverter.Format != destinationFormat ||
                        _formatConverterSourceFormat != sourceFormat)
                    {
                        WritePreamble();
                        _formatConverterSourceFormat = sourceFormat;
                        _formatConverter = new FormatConvertedBitmap(source, destinationFormat, Palette, 0);
                        //WritePostscript();//. This Is What I Did. But Think This Is Neccessary.
                    }

                    convertedSource = _formatConverter;
                }

                convertedSource.CopyPixels(sourceRect, buffer, bufferSize, stride);
            }
        }

But even if I want to repro as you mentioned, removing WritePostscript is unsafe i think.

And that Unsatefty make me worry to make error.

So right now I try to do everything to get the reason WHY?

Coordinator
Nov 3, 2011 at 3:08 AM

I'm sorry we are miscommunicating.  I am NOT asking you to fix any code in my project.  I am only asking you to show me your test program (how you are USING my project).

For example, you showed some code like:

  if(_nTime % 10 == 0)//. per 10 seconds update Image
  {
     BitmapSource bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                _hBitmapStaff,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

     ColorKeyBitmap colorKeyBmp = new ColorKeyBitmap();
     colorKeyBmp.TransparentColor = Color.FromArgb(255, 255, 255, 255);
     colorKeyBmp.Source = bitSrc;
     img.Source = colorKeyBmp;
  }

  if(_nTime % 2 == 0)//. per 2 seconds update TextBlock
  {
      text.Text = String.Format("{0}", _nTime);
      //text.invalidateVisible();
      //this.invalidateVisible();
  }

But this code snippet leaves me with questions:

1) What kind of timer are you creating?
2) What is hBitmapStaff?
3) Is anything else interesting happening?

If you can just zip up your test program and send it to me, I should be able to determine why it is not working.