2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Threading;
20 using System.Threading.Tasks;
24 public class Image : Widget
26 bool _canScaleUp = true;
27 bool _canScaleDown = true;
28 Interop.SmartEvent _clicked;
30 public Image(EvasObject parent) : base(parent)
32 _clicked = new Interop.SmartEvent(this, Handle, "clicked");
33 _clicked.On += (s, e) => Clicked?.Invoke(this, EventArgs.Empty);
36 public event EventHandler Clicked;
37 public event EventHandler LoadingCompleted;
38 public event EventHandler LoadingFailed;
44 return Interop.Elementary.elm_image_file_get(Handle);
52 return Interop.Elementary.elm_image_smooth_get(Handle);
56 Interop.Elementary.elm_image_smooth_set(Handle, value);
64 return !Interop.Elementary.elm_image_no_scale_get(Handle);
68 Interop.Elementary.elm_image_no_scale_set(Handle, !value);
72 public bool CanScaleDown
80 _canScaleDown = value;
81 Interop.Elementary.elm_image_resizable_set(Handle, _canScaleUp, _canScaleDown);
85 public bool CanScaleUp
94 Interop.Elementary.elm_image_resizable_set(Handle, _canScaleUp, _canScaleDown);
98 public bool CanFillOutside
102 return Interop.Elementary.elm_image_fill_outside_get(Handle);
106 Interop.Elementary.elm_image_fill_outside_set(Handle, value);
110 public int PrescaleSize
114 return Interop.Elementary.elm_image_prescale_get(Handle);
118 Interop.Elementary.elm_image_prescale_set(Handle, value);
122 public bool IsFixedAspect
126 return Interop.Elementary.elm_image_aspect_fixed_get(Handle);
130 Interop.Elementary.elm_image_aspect_fixed_set(Handle, value);
134 public bool IsAnimated
138 return Interop.Elementary.elm_image_animated_get(Handle);
142 Interop.Elementary.elm_image_animated_set(Handle, value);
146 public bool IsAnimatedAvailable
150 return Interop.Elementary.elm_image_animated_available_get(Handle);
154 public bool IsAnimationPlaying
158 return Interop.Elementary.elm_image_animated_play_get(Handle);
162 Interop.Elementary.elm_image_animated_play_set(Handle, value);
166 public bool IsEditable
170 return Interop.Elementary.elm_image_editable_get(Handle);
174 Interop.Elementary.elm_image_editable_set(Handle, value);
178 public Size ObjectSize
183 Interop.Elementary.elm_image_object_size_get(Handle, out w, out h);
184 return new Size(w, h);
192 IntPtr evasObj = Interop.Elementary.elm_image_object_get(Handle);
193 if (evasObj != IntPtr.Zero)
195 return !Interop.Evas.evas_object_image_alpha_get(evasObj);
201 IntPtr evasObj = Interop.Elementary.elm_image_object_get(Handle);
202 if (evasObj != IntPtr.Zero)
204 Interop.Evas.evas_object_image_alpha_set(evasObj, !value);
209 public ImageOrientation Orientation
213 return (ImageOrientation)Interop.Elementary.elm_image_orient_get(Handle);
217 Interop.Elementary.elm_image_orient_set(Handle, (int)value);
221 public override Color Color
225 int r = 255, g = 255, b = 255, a = 255;
226 IntPtr evasObj = Interop.Elementary.elm_image_object_get(Handle);
227 if (evasObj != IntPtr.Zero)
229 Interop.Evas.evas_object_color_get(evasObj, out r, out g, out b, out a);
231 return Color.FromRgba(r, g, b, a);
235 IntPtr evasObj = Interop.Elementary.elm_image_object_get(Handle);
236 if (evasObj != IntPtr.Zero)
238 Interop.Evas.evas_object_color_set(evasObj, value.R, value.G, value.B, value.A);
243 public bool Load(string file)
246 throw new ArgumentNullException("file");
248 Interop.Elementary.elm_image_async_open_set(Handle, false);
249 Interop.Elementary.elm_image_preload_disabled_set(Handle, true);
250 return Interop.Elementary.elm_image_file_set(Handle, file, null);
253 public bool Load(Uri uri)
256 throw new ArgumentNullException("uri");
258 return Load(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri);
261 [CLSCompliant(false)]
262 [Obsolete("This method will be removed. Use Load(Stream stream) instead.")]
263 public unsafe bool Load(byte* img, long size)
266 throw new ArgumentNullException("img");
268 Interop.Elementary.elm_image_async_open_set(Handle, false);
269 Interop.Elementary.elm_image_preload_disabled_set(Handle, true);
270 return Interop.Elementary.elm_image_memfile_set(Handle, img, size, IntPtr.Zero, IntPtr.Zero);
273 public bool Load(Stream stream)
276 throw new ArgumentNullException("stream");
278 Interop.Elementary.elm_image_async_open_set(Handle, false);
279 Interop.Elementary.elm_image_preload_disabled_set(Handle, true);
280 MemoryStream memstream = new MemoryStream();
281 stream.CopyTo(memstream);
284 byte[] dataArr = memstream.ToArray();
285 fixed (byte* data = &dataArr[0])
287 return Interop.Elementary.elm_image_memfile_set(Handle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
292 public Task<bool> LoadAsync(string file, CancellationToken cancellationToken = default(CancellationToken))
295 throw new ArgumentNullException("file");
297 Interop.Elementary.elm_image_async_open_set(Handle, true);
298 Interop.Elementary.elm_image_preload_disabled_set(Handle, false);
300 var tcs = new TaskCompletionSource<bool>();
302 cancellationToken.Register(() =>
304 if (tcs != null && !tcs.Task.IsCompleted)
310 Interop.SmartEvent loadReady = new Interop.SmartEvent(this, Handle, "load,ready");
311 loadReady.On += (s, e) =>
314 LoadingCompleted?.Invoke(this, EventArgs.Empty);
315 if (tcs != null && !tcs.Task.IsCompleted)
321 Interop.SmartEvent loadError = new Interop.SmartEvent(this, Handle, "load,error");
322 loadError.On += (s, e) =>
325 LoadingFailed?.Invoke(this, EventArgs.Empty);
326 if (tcs != null && !tcs.Task.IsCompleted)
328 tcs.SetResult(false);
332 bool ret = Interop.Elementary.elm_image_file_set(Handle, file, null);
335 throw new InvalidOperationException("Failed to set file to Image");
341 public Task<bool> LoadAsync(Uri uri, CancellationToken cancellationToken = default(CancellationToken))
344 throw new ArgumentNullException("uri");
346 return LoadAsync(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri, cancellationToken);
349 public async Task<bool> LoadAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
352 throw new ArgumentNullException("stream");
354 Interop.Elementary.elm_image_async_open_set(Handle, true);
355 Interop.Elementary.elm_image_preload_disabled_set(Handle, false);
357 var tcs = new TaskCompletionSource<bool>();
359 cancellationToken.Register(() =>
361 if (tcs != null && !tcs.Task.IsCompleted)
367 Interop.SmartEvent loadReady = new Interop.SmartEvent(this, Handle, "load,ready");
368 loadReady.On += (s, e) =>
371 LoadingCompleted?.Invoke(this, EventArgs.Empty);
372 if (tcs != null && !tcs.Task.IsCompleted)
378 Interop.SmartEvent loadError = new Interop.SmartEvent(this, Handle, "load,error");
379 loadError.On += (s, e) =>
382 LoadingFailed?.Invoke(this, EventArgs.Empty);
383 if (tcs != null && !tcs.Task.IsCompleted)
385 tcs.SetResult(false);
389 MemoryStream memstream = new MemoryStream();
390 await stream.CopyToAsync(memstream);
394 byte[] dataArr = memstream.ToArray();
395 fixed (byte* data = &dataArr[0])
397 bool ret = Interop.Elementary.elm_image_memfile_set(Handle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
405 return await tcs.Task;
408 protected override IntPtr CreateHandle(EvasObject parent)
410 return Interop.Elementary.elm_image_add(parent.Handle);
414 public enum ImageOrientation : int