質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

8271閲覧

エラーに困っています。:インデックスが範囲を超えています。負でない値で、コレクションのサイズよりも小さくなければなりません。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2019/12/12 01:16

#エラーメッセージに対処できず、困っています。

###エラーメッセージ
System.ArgumentOutOfRangeException: 'インデックスが範囲を超えています。負でない値で、コレクションのサイズよりも小さくなければなりません。

####開発環境
windows10
Visual Studio 2019

###作りたいもの
画像1,画像2を用意し、画像1の明るさを画像2に取り込みたい。

###方針
画像1,2ともにGetPixelでピクセル値を取得し、System.Drawing.ColorクラスでRGBの値をもらいい、輝度値や色差信号C1,C2を求める。
画像2には、輝度値だけを画像1からもらい、RGBを計算しなおし、SetPixelで表示したいと考えています。

###ソースコード

C#

1using System; 2using System.Collections; 3 4using System.Collections.Generic; 5using System.Linq; 6using System.Text; 7using System.Threading.Tasks; 8using System.Windows; 9using System.Windows.Controls; 10using System.Windows.Data; 11using System.Windows.Documents; 12using System.Windows.Input; 13using System.Windows.Media; 14using System.Windows.Media.Imaging; 15using System.Windows.Navigation; 16using System.Windows.Shapes; 17using Microsoft.Win32; 18using System.IO; 19using System.Drawing; 20using System.Windows.Interop; 21 22namespace WpfApplication_imageAtmosphere 23{ 24 /// <summary> 25 /// MainWindow.xaml の相互作用ロジック 26 /// </summary> 27 public partial class MainWindow : Window 28 { 29 30 public string ImageFileSourcePath01 = null; 31 public string ImageFileSourcePath02 = null; 32 33 int Image01_width, Image01_height; 34 35 public MainWindow() 36 { 37 InitializeComponent(); 38 } 39 40 //System.Drawing.BitmapからImageSourceへ変換する 41 //ここから 42 [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")] 43 [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] 44 public static extern bool DeleteObject([System.Runtime.InteropServices.In] IntPtr hObject); 45 46 public static ImageSource ToImageSource(Bitmap bmp) 47 { 48 var handle = bmp.GetHbitmap(); 49 try 50 { 51 return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 52 } 53 finally { DeleteObject(handle); } 54 } 55 //ここまで 56 57 58 59 private void open_button_Click(object sender, RoutedEventArgs e) 60 { 61 OpenFileDialog openFileDialog = new OpenFileDialog(); 62 if (openFileDialog.ShowDialog() == true) 63 { 64 ImageFileSourcePath01 = openFileDialog.FileName; 65 66 } 67 BitmapImage bitmapImage = new BitmapImage(new Uri(ImageFileSourcePath01)); 68 int Image01_width = bitmapImage.PixelWidth; 69 int Image01_height = bitmapImage.PixelHeight; 70 imageBox01.Source = bitmapImage; 71 72 } 73 74 75 76 77 78 private void image_atomsphere_Click(object sender, RoutedEventArgs e) 79 { 80 81 82 OpenFileDialog openFileDialog = new OpenFileDialog(); 83 if (openFileDialog.ShowDialog() == true) 84 { 85 ImageFileSourcePath02 = openFileDialog.FileName; 86 87 } 88 BitmapImage bitmapImage = new BitmapImage(new Uri(ImageFileSourcePath02)); 89 90 91 //画像2の大きさ 92 int Image02_width = bitmapImage.PixelWidth; 93 int Image02_height = bitmapImage.PixelHeight; 94 imageBox02.Source = bitmapImage; 95 96 97 int i, j, k,l,nx1, ny1,x,y; 98 int nx2, ny2; 99 100 double light, c1, c2; 101 double rr, gg, bb; 102 double rrr, ggg, bbb; 103 System.Drawing.Color col; 104 System.Drawing.Color colo; 105 106 double yy, c11,c22; 107 108 nx1 = Image01_width; 109 ny1 = Image01_height; 110 111 nx2 = Image02_width; 112 ny2 = Image02_height; 113 114 //代入の際のリストを作る 115 ArrayList list_c1 = new ArrayList(); 116 ArrayList list_c2 = new ArrayList(); 117 ArrayList list_c11 = new ArrayList(); 118 ArrayList list_c22 = new ArrayList(); 119 ArrayList list_light = new ArrayList(); 120 121 //ビットマップメソッドの実行 122 Bitmap bmp1 = new Bitmap(ImageFileSourcePath01); 123 Bitmap bmp2 = new Bitmap(ImageFileSourcePath02); 124 Bitmap bmp3 = new Bitmap(nx2, ny2); 125 126 127 //SetPixelの値をrd,grn,blにする 128 double rd, grn, bl; 129 130 131 for (l = 0; l < ny2; l++) 132 for (k = 0; k < ny2; k++) 133 { 134 colo = bmp2.GetPixel(k, l); 135 rrr = colo.R; ggg = colo.G; bbb = colo.B; 136 137 yy = 0.299 * rrr + 0.587 * ggg + 0.114 * bbb; 138 c11 = rrr - yy; 139 c22 = bbb - yy; 140 141 list_c11.Add(c11); 142 list_c22.Add(c22); 143 } 144 145 146 147 148 for (j = 0; j< ny1; j++) 149 for(i = 0;i < nx1; i++) 150 { 151 col = bmp1.GetPixel(i, j); 152 rr = col.R; gg = col.G; bb = col.B; 153 154 light = 0.299 * rr + 0.587 * gg + 0.114 * bb; 155 c1 = rr - light; 156 c2 = bb - light; 157 158 list_light.Add(light); 159 list_c1.Add(c1); 160 list_c2.Add(c2); 161 } 162 163 164 165 166 for (y = 0; y < ny2; y++) 167 for(x = 0; x < nx2; x++) 168 { 169           //ここで怒られています。 170 rd = Convert.ToDouble(list_light[x + y]) + Convert.ToDouble(list_c11[x + y]); 171 grn = Convert.ToDouble(list_light[x + y]) - ((0.299 * Convert.ToDouble(list_c11[x + y]) + 0.114 * Convert.ToDouble(list_c22[x + y])) / 0.587); 172 bl = Convert.ToDouble(list_light[x + y]) + Convert.ToDouble(list_c22[x + y]); 173 174 if (rd < 0) rd = 0; 175 if (rd > 255) rd = 255; 176 if (grn < 0) grn = 0; 177 if (grn > 255) grn = 255; 178 if (bl < 0) bl = 0; 179 if (bl > 255) bl = 255; 180 181 bmp3.SetPixel(x, y, System.Drawing.Color.FromArgb((int)rd, (int)grn, (int)bl)); 182 183 } 184 185 imageBox02.Source = ToImageSource(bmp3); 186 187 } 188 189 190 191 192 193 194 195 196 } 197} 198

XMAL

1<Window x:Class="WpfApplication_imageAtmosphere.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication_imageAtmosphere" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="auto"/> 12 <RowDefinition/> 13 <RowDefinition/> 14 </Grid.RowDefinitions> 15 16 <Grid.ColumnDefinitions> 17 <ColumnDefinition/> 18 <ColumnDefinition/> 19 </Grid.ColumnDefinitions> 20 21 <Menu> 22 <MenuItem Header="ファイル"> 23 <MenuItem Header="開く" Name="open_button" Click="open_button_Click"/> 24 25 </MenuItem> 26 <MenuItem Header="編集"> 27 <MenuItem Header="画像雰囲気" Name="image_atomsphere" Click="image_atomsphere_Click"/> 28 29 </MenuItem> 30 </Menu> 31 32 <Image Source="Resources\noimage.png" Name="imageBox01" Grid.Row="1"/> 33 <Image Source="Resources\noimage.png" Name="imageBox02" Grid.Row="1" Grid.Column="1"/> 34 35 <Image Source="Resources\noimage.png" Name="imageBox3" Grid.Row="2" Grid.ColumnSpan="2"/> 36 37 </Grid> 38</Window> 39

##エラーメッセージの該当場所

C#

1rd = Convert.ToDouble(list_light[x + y]) + Convert.ToDouble(list_c11[x + y]);

どうかご教授よろしくお願いします。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

azuapricot

2019/12/12 01:17

デバッグはしてみましたか?
m.ts10806

2019/12/12 01:20

エラーの意味が理解できたら自ずと見る場所対処方法は決まってきます。 どこまで調べたのでしょうか
退会済みユーザー

退会済みユーザー

2019/12/12 01:27

list_c1、list_c2に値がなぜか入っていませんでした。調べてみます。 ありがとうございます。
m.ts10806

2019/12/12 01:37

「調べてみます」で解決では同じような問題を抱えた人の参考にならないので、本当に解決したらその解決方法を以て解決済みとしてください
guest

回答1

0

ベストアンサー

その例外が出たときの

rd = Convert.ToDouble(list_light[x + y]) + Convert.ToDouble(list_c11[x + y]);

xとyの値、そして、list_lightとlist_c11のサイズをを調べてみては?

投稿2019/12/12 01:21

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/12/12 01:28

わかりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問