diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2015-06-02 10:44:23 +0200 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2015-06-02 10:44:23 +0200 |
| commit | b56c22ad826a49b7ea0013c1ca78cf64169fc326 (patch) | |
| tree | 588a3bc9ae0ed826012096a072b138a94ded4e61 /src/GraphicObjects/Image.cs | |
| parent | 999f18b6daf7db54879a85f0f8b5c7b22f740ce7 (diff) | |
TemplatedControls, Picture objects for pixmap and svg raw data sharing
among controls
Diffstat (limited to 'src/GraphicObjects/Image.cs')
| -rwxr-xr-x | src/GraphicObjects/Image.cs | 131 |
1 files changed, 27 insertions, 104 deletions
diff --git a/src/GraphicObjects/Image.cs b/src/GraphicObjects/Image.cs index 863c95cc..2cb70446 100755 --- a/src/GraphicObjects/Image.cs +++ b/src/GraphicObjects/Image.cs @@ -12,18 +12,14 @@ namespace go {
public class Image : GraphicObject
{
- byte[] image;
- Rsvg.Handle hSVG;
- Size imgSize;
- string _imgPath;
+ Picture _pic;
string _svgSub;
-
+
[XmlAttributeAttribute("Path")]
public string ImagePath {
- get { return _imgPath; }
- set {
- _imgPath = value;
- LoadImage (_imgPath);
+ get { return _pic == null ? null : _pic.Path; }
+ set {
+ LoadImage (value);
}
}
@@ -40,127 +36,54 @@ namespace go public Image () : base()
{
}
- public Image (string ImagePath, Rectangle _bounds)
+ public Image (string _imgPath, Rectangle _bounds)
: base(_bounds)
- {
- _imgPath = ImagePath;
+ {
LoadImage(_imgPath);
}
- public Image (string ImagePath)
+ public Image (string _imgPath)
: base()
- {
- _imgPath = ImagePath;
+ {
LoadImage(_imgPath);
}
- public Image (System.Drawing.Bitmap _bitmap)
- : base()
- {
- LoadImage (_bitmap);
- }
+// public Image (System.Drawing.Bitmap _bitmap)
+// : base()
+// {
+// _pic = new BmpPicture ();
+//
+// LoadImage (_bitmap);
+// }
#endregion
#region Image Loading
- void loadFromRessource(string resId)
- {
- Stream stream = null;
-
- //first, search for ressource in main executable assembly
- stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId);
- if (stream == null)//try to find ressource in golib assembly
- stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId);
- if (stream == null)
- return;
-
- using (MemoryStream ms = new MemoryStream ()) {
- stream.CopyTo (ms);
-
- if (resId.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) {
- hSVG = new Rsvg.Handle (ms.ToArray ());
- imgSize = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
- } else
- LoadImage (new System.Drawing.Bitmap (ms));
- }
- }
- void loadFromFile(string path)
- {
- if (!File.Exists(path))
- return;
-
- if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) {
- hSVG = new Rsvg.Handle (path);
- imgSize = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
- }else
- LoadImage (new System.Drawing.Bitmap (path));
-
- }
public void LoadImage (string path)
{
- hSVG = null;
- image = null;
- if (path.StartsWith ("#"))
- loadFromRessource (path.Substring (1));
- else
- loadFromFile (path);
+ if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture))
+ _pic = new SvgPicture ();
+ else
+ _pic = new BmpPicture ();
- _imgPath = path;
- }
- //load image via System.Drawing.Bitmap, cairo load png only
- public void LoadImage (System.Drawing.Bitmap bitmap)
- {
- if (bitmap == null)
- return;
-
- System.Drawing.Imaging.BitmapData data = bitmap.LockBits
- (new System.Drawing.Rectangle (0, 0, bitmap.Width, bitmap.Height),
- System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
-
- imgSize = new Size (bitmap.Width, bitmap.Height);
-
- int stride = data.Stride;
- int bitmapSize = Math.Abs (data.Stride) * bitmap.Height;
-
- image = new byte[bitmapSize];
- System.Runtime.InteropServices.Marshal.Copy (data.Scan0, image, 0, bitmapSize);
-
- bitmap.UnlockBits (data);
+ _pic.LoadImage (path);
}
#endregion
#region GraphicObject overrides
protected override Size measureRawSize ()
{
- if (image == null && hSVG == null) {
- loadFromRessource ("go.Images.Icons.IconAlerte.svg");
- }
+// if (_pic == null)
+// _pic = "#go.Images.Icons.IconAlerte.svg";
- return imgSize + Margin * 2;
+ return _pic.Dimensions + Margin * 2;
}
protected override void onDraw (Context gr)
{
base.onDraw (gr);
- float widthRatio = (float)ClientRectangle.Width / imgSize.Width;
- float heightRatio = (float)ClientRectangle.Height / imgSize.Height;
- float ratio = Math.Min (widthRatio, heightRatio);
+ if (_pic == null)
+ return;
- Rectangle rImg = ClientRectangle;
- gr.Save ();
- gr.Scale (widthRatio, heightRatio);
- if (hSVG == null) {
- using (ImageSurface imgSurf = new ImageSurface (image, Format.Argb32,
- imgSize.Width, imgSize.Height, 4 * imgSize.Width)) {
- gr.SetSourceSurface (imgSurf, (int)(rImg.X / widthRatio), (int)(rImg.Y / heightRatio));
- gr.Paint ();
- }
- } else {
- gr.Translate (rImg.X/widthRatio, rImg.Y/heightRatio);
- if (string.IsNullOrEmpty (_svgSub))
- hSVG.RenderCairo (gr);
- else
- hSVG.RenderCairoSub (gr, "#" + _svgSub);
- }
- gr.Restore ();
+ _pic.Paint (gr, ClientRectangle, _svgSub);
}
#endregion
}
|