none
如何得到剪贴板中图片的长度和宽度?(C++) RRS feed

  • 问题

  • 我的代码是:

    		Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    		auto b = a->GetContent();
    		if (!b->Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::Bitmap)){
    			return;
    		}
    		create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    			try{
    				create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    					try{
    						auto m_bm = ref new WriteableBitmap(1, 1);
    						m_bm->SetSource(d);
    						wb_ = ref new WriteableBitmap(m_bm->PixelWidth, m_bm->PixelHeight);
    						unsigned int length;
    						byte* temp_m_bm = GetPointerToPixelData(m_bm->PixelBuffer, &length);
    						byte* temp_wb_ = GetPointerToPixelData(wb_->PixelBuffer, &length);
    						for (unsigned int k = 0; k <unsigned int(height); k++){
    							for (unsigned int i = 0; i < unsigned int(width * 4); i += 4){
    								int pos = k * (width * 4) + (i);
    								temp_wb_[pos + 0] = temp_m_bm[pos + 0];
    								temp_wb_[pos + 1] = temp_m_bm[pos + 1];
    								temp_wb_[pos + 2] = temp_m_bm[pos + 2];
    								temp_wb_[pos + 3] = temp_m_bm[pos + 3];
    							}
    						}
    						Pic->Source = wb_;
    					}
    					catch (int c){
    						c++;
    					}
    				});
    			}
    			catch (int c){
    				c++;
    			}
    		});

    但是过程中我无法直接知道b的图片长宽,而是分别转换到c和d以后进行了setsource才知道的。请问怎样才能直接得知。

    (这段代码在调试中没有问题,但是到了非调试模式会退出(返回到开始屏幕))


    2014年7月19日 1:07

答案

  • 你好 lxylxy123456,

    DataPackageView中返回的值仅仅是一个Image的流,所以是没办法直接获取image的大小,必须把image从流转化出来才可以。

    可以直接通过 m_bm->PixelHeight来获取高度,PixelWidth来获取长度。

    另外我这里运行我下面代码的时候不会崩溃,你试试看。

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	if (!b->Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::Bitmap)){
    		return;
    	}
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    		try{
    			create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    				try{
    					auto m_bm = ref new WriteableBitmap(1, 1);
    					m_bm->SetSource(d);
    
    					auto height = m_bm->PixelHeight;
    					auto width = m_bm->PixelWidth;
    
    					//auto wb_ = ref new WriteableBitmap(m_bm->PixelWidth, m_bm->PixelHeight);
    					//unsigned int length;
    					//byte* temp_m_bm = GetPointerToPixelData(m_bm->PixelBuffer, &length);
    					//byte* temp_wb_ = GetPointerToPixelData(wb_->PixelBuffer, &length);
    					//for (unsigned int k = 0; k <unsigned int(height); k++){
    					//	for (unsigned int i = 0; i < unsigned int(width * 4); i += 4){
    					//		int pos = k * (width * 4) + (i);
    					//		temp_wb_[pos + 0] = temp_m_bm[pos + 0];
    					//		temp_wb_[pos + 1] = temp_m_bm[pos + 1];
    					//		temp_wb_[pos + 2] = temp_m_bm[pos + 2];
    					//		temp_wb_[pos + 3] = temp_m_bm[pos + 3];
    					//	}
    					//}
    					//Pic->Source = wb_;
    				}
    				catch (int c){
    					c++;
    				}
    			});
    		}
    		catch (int c){
    			c++;
    		}
    	});
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年7月21日 6:29
    版主

全部回复

  • 你好 lxylxy123456,

    DataPackageView中返回的值仅仅是一个Image的流,所以是没办法直接获取image的大小,必须把image从流转化出来才可以。

    可以直接通过 m_bm->PixelHeight来获取高度,PixelWidth来获取长度。

    另外我这里运行我下面代码的时候不会崩溃,你试试看。

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	if (!b->Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::Bitmap)){
    		return;
    	}
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    		try{
    			create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    				try{
    					auto m_bm = ref new WriteableBitmap(1, 1);
    					m_bm->SetSource(d);
    
    					auto height = m_bm->PixelHeight;
    					auto width = m_bm->PixelWidth;
    
    					//auto wb_ = ref new WriteableBitmap(m_bm->PixelWidth, m_bm->PixelHeight);
    					//unsigned int length;
    					//byte* temp_m_bm = GetPointerToPixelData(m_bm->PixelBuffer, &length);
    					//byte* temp_wb_ = GetPointerToPixelData(wb_->PixelBuffer, &length);
    					//for (unsigned int k = 0; k <unsigned int(height); k++){
    					//	for (unsigned int i = 0; i < unsigned int(width * 4); i += 4){
    					//		int pos = k * (width * 4) + (i);
    					//		temp_wb_[pos + 0] = temp_m_bm[pos + 0];
    					//		temp_wb_[pos + 1] = temp_m_bm[pos + 1];
    					//		temp_wb_[pos + 2] = temp_m_bm[pos + 2];
    					//		temp_wb_[pos + 3] = temp_m_bm[pos + 3];
    					//	}
    					//}
    					//Pic->Source = wb_;
    				}
    				catch (int c){
    					c++;
    				}
    			});
    		}
    		catch (int c){
    			c++;
    		}
    	});
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年7月21日 6:29
    版主
  • 我突然发现没问题了,谢谢解答!如果再次出现问题我会告诉你。

    2014年7月22日 8:11