|
| 1 | +#include "pch.h" |
| 2 | +#include "BikaImage.h" |
| 3 | +#if __has_include("BikaImage.g.cpp") |
| 4 | +#include "BikaImage.g.cpp" |
| 5 | +#endif |
| 6 | + |
| 7 | +using namespace winrt; |
| 8 | +using namespace Windows::UI::Xaml; |
| 9 | +using namespace Windows::Foundation; |
| 10 | +using namespace Windows::Storage::Streams; |
| 11 | +namespace winrt::bikabika::implementation |
| 12 | +{ |
| 13 | + BikaImage::BikaImage() |
| 14 | + { |
| 15 | + DefaultStyleKey(winrt::box_value(L"bikabika.BikaImage")); |
| 16 | + InitializeComponent(); |
| 17 | + } |
| 18 | + |
| 19 | + Windows::UI::Xaml::DependencyProperty BikaImage::m_imageProperty = |
| 20 | + Windows::UI::Xaml::DependencyProperty::Register( |
| 21 | + L"ImgBlock", |
| 22 | + winrt::xaml_typename<winrt::BikaClient::Blocks::ImageBlock>(), |
| 23 | + winrt::xaml_typename<bikabika::BikaImage>(), |
| 24 | + Windows::UI::Xaml::PropertyMetadata{ nullptr } |
| 25 | + ); |
| 26 | + winrt::BikaClient::Blocks::ImageBlock BikaImage::ImgBlock() |
| 27 | + { |
| 28 | + return winrt::unbox_value<winrt::BikaClient::Blocks::ImageBlock>(GetValue(m_imageProperty)); |
| 29 | + } |
| 30 | + |
| 31 | + void BikaImage::ImgBlock(winrt::BikaClient::Blocks::ImageBlock const& value) |
| 32 | + { |
| 33 | + SetValue(m_imageProperty, winrt::box_value(value)); |
| 34 | + BikaImg().Source(value.Img()); |
| 35 | + } |
| 36 | + |
| 37 | + Windows::UI::Xaml::Media::ImageSource BikaImage::ImgSource() |
| 38 | + { |
| 39 | + return BikaImg().Source(); |
| 40 | + } |
| 41 | + void BikaImage::ImageLoaded(bool const& value) |
| 42 | + { |
| 43 | + m_loaded = value; |
| 44 | + } |
| 45 | + bool BikaImage::ImageLoaded() |
| 46 | + { |
| 47 | + return m_loaded; |
| 48 | + } |
| 49 | +} |
| 50 | +void winrt::bikabika::implementation::BikaImage::BikaImg_ImageFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::ExceptionRoutedEventArgs const& e) |
| 51 | +{ |
| 52 | + ImageLoaded(false); |
| 53 | + RefreshIcon().Visibility(Visibility::Visible); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +Windows::Foundation::IAsyncAction winrt::bikabika::implementation::BikaImage::BikaImg_ImageOpened(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e) |
| 58 | +{ |
| 59 | + ImageLoaded(true); |
| 60 | + RefreshIcon().Visibility(Visibility::Collapsed); |
| 61 | + // 通过Xaml界面获取位图的大小不对 |
| 62 | + //winrt::BikaClient::Blocks::ImageBlock imgBlock = ImgBlock(); |
| 63 | + //winrt::Windows::UI::Xaml::Media::Imaging::RenderTargetBitmap bitmap; |
| 64 | + //co_await bitmap.RenderAsync(sender.as<UIElement>()); |
| 65 | + //IBuffer pixels = co_await bitmap.GetPixelsAsync(); |
| 66 | + |
| 67 | + //Windows::Storage::ApplicationDataContainer settings = Windows::Storage::ApplicationData::Current().LocalSettings().CreateContainer(L"Settings", Windows::Storage::ApplicationDataCreateDisposition::Always); |
| 68 | + //if (!settings.Values().HasKey(L"CacheFolder")) |
| 69 | + //{ |
| 70 | + // Windows::Storage::StorageFolder storageFolder{ Windows::Storage::ApplicationData::Current().LocalFolder() }; |
| 71 | + // Windows::Storage::StorageFolder downloadFolder = co_await storageFolder.CreateFolderAsync(L"Cache"); |
| 72 | + // settings.Values().Insert(L"CacheFolder", box_value(downloadFolder.Path())); |
| 73 | + //} |
| 74 | + //Windows::Storage::StorageFolder storageFolder = co_await Windows::Storage::StorageFolder::GetFolderFromPathAsync(settings.Values().Lookup(L"CacheFolder").as<hstring>()); |
| 75 | + //Windows::Storage::StorageFile imgFile = co_await storageFolder.CreateFileAsync(imgBlock.OriginalName(), Windows::Storage::CreationCollisionOption::ReplaceExisting); |
| 76 | + //Windows::Storage::Streams::IRandomAccessStream stream{ co_await imgFile.OpenAsync(Windows::Storage::FileAccessMode::ReadWrite) }; |
| 77 | + //auto softwareBitmap = winrt::Windows::Graphics::Imaging::SoftwareBitmap::CreateCopyFromBuffer(pixels, winrt::Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, (uint32_t)200, (uint32_t)200, winrt::Windows::Graphics::Imaging::BitmapAlphaMode::Ignore); |
| 78 | + //auto propertySet = winrt::Windows::Graphics::Imaging::BitmapPropertySet(); |
| 79 | + //winrt::Windows::Graphics::Imaging::BitmapTypedValue qualityValue(box_value(1.0), Windows::Foundation::PropertyType::Single); |
| 80 | + //propertySet.Insert(L"ImageQuality", qualityValue); |
| 81 | + //winrt::Windows::Graphics::Imaging::BitmapEncoder encoder = co_await winrt::Windows::Graphics::Imaging::BitmapEncoder::CreateAsync(winrt::Windows::Graphics::Imaging::BitmapEncoder::JpegEncoderId(), stream, propertySet); |
| 82 | + //encoder.SetSoftwareBitmap(softwareBitmap); |
| 83 | + ///*auto dataReader = DataReader::FromBuffer(pixels); |
| 84 | + //winrt::array_view<byte> b; |
| 85 | + //dataReader.ReadBytes(b); |
| 86 | + //encoder.SetPixelData(winrt::Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, |
| 87 | + // winrt::Windows::Graphics::Imaging::BitmapAlphaMode::Ignore, |
| 88 | + // (uint32_t)bitmap.PixelWidth(),(uint32_t)bitmap.PixelHeight(), |
| 89 | + // 96, 96,b);*/ |
| 90 | + //co_await encoder.FlushAsync(); |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void winrt::bikabika::implementation::BikaImage::RefreshIcon_PointerPressed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e) |
| 96 | +{ |
| 97 | + winrt::BikaClient::Blocks::ImageBlock imgBlock = ImgBlock(); |
| 98 | + BikaImg().Source(imgBlock.Img()); |
| 99 | +} |
0 commit comments