DrawingVisual 内の画像ソースのトリミングされた領域を表示する必要がありますが、過去数時間、単純なタスクの空白を描画していました。
私の計画は、さまざまなフレームを含む大きな画像を取得する (HMI システム用の) 時間指定アニメーションを実装することです。状態変数に応じて、DrawingVisual はこの大きな画像から現在のフレームを抽出して表示するため、トリミングが必要になります。
私のコードは次のとおりです。
int width = Convert.ToInt32(_imageSource.Width / 2);
int height = Convert.ToInt32(_imageSource.Height);
BitmapImage bm = _imageSource.Clone() as BitmapImage;
bm.BaseUri = BaseUriHelper.GetBaseUri(this);
CroppedBitmap c = new CroppedBitmap(bm, new Int32Rect(0, 0, width, height));
ImageDrawing id = new ImageDrawing(c, new Rect(0, 0, width, height));
dg.Children.Add(id);
dc.DrawDrawing(dg);
ただし、トリミングされた画像を作成すると、
「値が想定範囲内にありません」
ところで、画像はリソースディクショナリ内(現在のアセンブリ内)にロードされたpngです
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="elbowSmallBlueBL" UriSource="./Assets/HMI/Elbows/Small/Elbow Blue BottomLeft.png" />
(ImageSource)FindResource("... を使用して DrawingVisual に割り当てられます。
第一に、私はエラーメッセージを理解できず、満足のいく説明がどこにも見つかりません.第二に、なぜ単純なタスクを実行するのがとても難しいのですか?
助けてくれてありがとう。
psこれは、リソースを見つけることができないためだと思います。しかし、元のイメージソースに問題がない場合、クローン バージョンの動作が異なるのはなぜでしょうか? pps 行「bm.BaseUri = ...」を削除すると、同じエラーが発生します。
簡単にするために、クリックするとCroppedBitmapを作成しようとするボタンのみを持つテストプロジェクトを作成しました。
ボタンクリック=
private void Button_Click(object sender, RoutedEventArgs e)
{
BitmapSource i = (BitmapSource)FindResource("elbowSmallBlueBL");
try
{
CroppedBitmap c = new CroppedBitmap(i, new Int32Rect(0, 0, 100, 100));
}
catch (Exception ex)
{
}
}
}
リソース ディクショナリはビットマップを参照します。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Elbows -->
<BitmapImage x:Key="elbowSmallBlueBL" UriSource="./Assets/HMI/Elbows/Small/Elbow Blue BottomLeft.png" />
App.xamlが含まれています
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="rdHMIControls.xaml" />
BitmapSource (i) は有効で、CroppedBitmap への呼び出しは ArgumentException、「値が許容範囲内にありません」をスローします。
トリミングされた画像を作成する、または作成しようとすることは、これよりも簡単ではありませんか?
私がMSDNから取った別の例
BitmapSource i = (BitmapSource)this.FindResource("test");
try
{
// Create an Image element.
Image croppedImage = new Image();
croppedImage.Width = 200;
croppedImage.Margin = new Thickness(5);
// Create a CroppedBitmap based off of a xaml defined resource.
CroppedBitmap cb = new CroppedBitmap(
i,
new Int32Rect(30, 20, 105, 50)); //select region rect
croppedImage.Source = cb; //set image source to cropped
再び、新しい CroppedBitmap が InvalidArgument exceptionin をスローします
何かご意見は?
ありがとう