none
VC++2008编写的winform应用程序中lockbit方法问题 RRS feed

  • 常规讨论

  • 在使用VC++2008编写的winform应用程序中,添加了一个PictureBox控件,使用lockbits等方法实现某些功能后,图像还原不过去了,也就是实现不了想要的方法,下面是实现把彩色图像灰化的代码。请改正,谢谢。

    Bitmap^ image1 = gcnew Bitmap(this->OriginalPicture->Image);
                                 Rectangle rect = Rectangle(0, 0, image1->Width, image1->Height);
                  System::Drawing::Imaging::BitmapData^ bmpData =
                        image1->LockBits(rect, System::Drawing::Imaging::ImageLockMode::ReadWrite,
                        image1->PixelFormat);
                          IntPtr ptr = bmpData->Scan0;

                         int bytes = image1->Width * image1->Height*3;
        array<Byte>^rgbValues = gcnew array<Byte>(bytes);
        System::Runtime::InteropServices::Marshal::Copy(ptr, rgbValues,0, bytes);
           Console::WriteLine(rgbValues->Length);
         for(int count=0;count<rgbValues->Length ;count+=3)
        {
          int k=System::Convert ::ToInt32(rgbValues[count]* 0.299+rgbValues[count+1]*0.587+rgbValues[count+2]*0.114);
          rgbValues[count]=k;
          rgbValues[count+1]=k;
          rgbValues[count+2]=k;

        }

         System::Runtime::InteropServices::Marshal::Copy(rgbValues , 0, ptr, bytes);

                        image1->UnlockBits(bmpData);

                    this->ResultPicture->Image = image1;

    2010年10月2日 5:16