BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
OMERO.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Drawing.Imaging;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using System.Drawing;
12using BioLib;
13using Gdk;
14using Graphics = System.Drawing.Graphics;
15using OMERO = BioLib.OMERO;
16using System.Runtime.InteropServices;
17namespace BioCore
18{
19 public partial class OMERO : Form
20 {
21 public List<omero.gateway.model.DatasetData> datas = new List<omero.gateway.model.DatasetData>();
22 private List<ListViewItem> items = new List<ListViewItem>();
23 public int selectedIndex = 0;
24 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
25 public int Progress
26 {
27 get { return progressBar.Value; }
28 set
29 {
30 if(value < 100)
31 progressBar.Value = value;
32
33 }
34 }
35 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
36 public string Status
37 {
38 get { return statusLabel.Text; }
39 set { statusLabel.Text = value; }
40 }
41 public OMERO()
42 {
43 InitializeComponent();
44 Login log = new Login();
45 if (log.ShowDialog() == DialogResult.OK)
46 {
47 BioLib.OMERO.Connect(Login.host, Login.port, Login.username, Login.password);
48 Init();
49 }
50 }
51 private void Init()
52 {
53 BioLib.OMERO.ReConnect();
54 var dst = BioLib.OMERO.GetDatasetsData();
55 datas.AddRange(dst);
56 foreach (var src in dst)
57 {
58 if (src != null)
59 {
60 comboBox1.Items.Add(src.getName() + " " + src.getId());
61 }
62 }
63 }
64 public static Icon BytesToIcon(System.Drawing.Bitmap bmp)
65 {
66 IntPtr hIcon = bmp.GetHicon();
67 return Icon.FromHandle(hIcon);
68 }
69 public static System.Drawing.Bitmap RawBytesToBitmap(byte[] rawData, int width, int height, System.Drawing.Imaging.PixelFormat pixelFormat)
70 {
71 int bytesPerPixel = Image.GetPixelFormatSize(pixelFormat) / 8;
72 int stride = width * bytesPerPixel;
73
74 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, pixelFormat);
75
76 // Lock bitmap and write raw data
77 System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
78 new System.Drawing.Rectangle(0, 0, width, height),
79 (ImageLockMode)ImageLockMode.WriteOnly,
80 pixelFormat);
81
82 IntPtr ptr = bmpData.Scan0;
83
84 // Handle stride mismatch if necessary
85 if (bmpData.Stride == stride)
86 {
87 Marshal.Copy(rawData, 0, ptr, rawData.Length);
88 }
89 else
90 {
91 // Row-by-row copy for non-matching stride
92 for (int y = 0; y < height; y++)
93 {
94 int srcOffset = y * stride;
95 int destOffset = y * bmpData.Stride;
96 Marshal.Copy(rawData, srcOffset, ptr + destOffset, stride);
97 }
98 }
99
100 bmp.UnlockBits(bmpData);
101 return bmp;
102 }
103 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
104 {
105 listView1.Items.Clear();
106 items.Clear();
107 string[] sts = comboBox1.SelectedItem.ToString().Split(" ");
108 List<ListViewItem> lss = new List<ListViewItem>();
109 foreach (omero.gateway.model.DatasetData s in datas)
110 {
111 if (s.getId().ToString() == sts[1].ToString())
112 {
113 Status = "Getting dataset information.";
114 List<string> fs = new List<string>();
115 List<long> ids = BioLib.OMERO.GetDatasetIds(s.getId());
116 foreach (long id in ids)
117 {
118 fs.Add(BioLib.OMERO.GetNameFromID(id));
119 }
120 if (loadIconsBox.Checked)
121 {
122 Status = "Loading icons...";
123 // Create ImageList
124 ImageList ims = new ImageList
125 {
126 ImageSize = new System.Drawing.Size(64, 64), // Set icon size (width x height)
127 ColorDepth = ColorDepth.Depth32Bit // Set color depth
128 };
129 Dictionary<long, Pixbuf> dict = BioLib.OMERO.GetThumbnails(fs.ToArray(), 64, 64);
130 int i = 0;
131 Status = "Loading files...";
132 foreach (var item in ids)
133 {
134 foreach (var itm in dict)
135 {
136 if(item == itm.Key)
137 {
138 System.Drawing.Bitmap bm = PixbufToBitmap(itm.Value);
139 ims.Images.Add(BytesToIcon(bm));
140 ListViewItem li = new ListViewItem();
141 li.Text = fs[i];
142 li.ImageIndex = i;
143 items.Add(li);
144 Progress = (int)(((float)(i + 1) / (float)(ids.Count)) * 100);
145 i++;
146 }
147 }
148 }
149 listView1.SmallImageList = ims;
150 listView1.LargeImageList = ims;
151 listView1.View = View.LargeIcon;
152 listView1.Items.AddRange(items.ToArray());
153 Status = "";
154 Progress = 0;
155 return;
156 }
157 else
158 {
159 Status = "Loading files...";
160 int i = 0;
161 foreach (var item in ids)
162 {
163 ListViewItem li = new ListViewItem();
164 li.Text = fs[i];
165 items.Add(li);
166 Progress = (int)(((float)(i + 1) / (float)(ids.Count)) * 100);
167 i++;
168 }
169 listView1.View = View.List;
170 listView1.Items.AddRange(items.ToArray());
171 Status = "";
172 Progress = 0;
173 return;
174 }
175 }
176 }
177 }
178 public static System.Drawing.Bitmap PixbufToBitmap(Pixbuf pixbuf)
179 {
180 if (pixbuf == null)
181 throw new ArgumentNullException(nameof(pixbuf), "Pixbuf cannot be null.");
182
183 // Get Pixbuf properties
184 int width = pixbuf.Width;
185 int height = pixbuf.Height;
186 int rowstride = pixbuf.Rowstride;
187 bool hasAlpha = pixbuf.HasAlpha;
188 byte[] pixelData = pixbuf.PixelBytes.Data;
189
190 // Create a Bitmap
191 System.Drawing.Imaging.PixelFormat pixelFormat = hasAlpha ? System.Drawing.Imaging.PixelFormat.Format32bppArgb : System.Drawing.Imaging.PixelFormat.Format24bppRgb;
192 System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, pixelFormat);
193
194 // Lock the Bitmap's data
195 System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(
196 new System.Drawing.Rectangle(0, 0, width, height),
197 (System.Drawing.Imaging.ImageLockMode)ImageLockMode.WriteOnly,
198 (System.Drawing.Imaging.PixelFormat)pixelFormat);
199
200 // Copy pixel data
201 IntPtr bmpPtr = bmpData.Scan0;
202 unsafe
203 {
204 byte* srcPtr = (byte*)pixbuf.Pixels;
205 byte* destPtr = (byte*)bmpPtr;
206
207 for (int y = 0; y < height; y++)
208 {
209 byte* srcRow = srcPtr + y * rowstride;
210 byte* destRow = destPtr + y * bmpData.Stride;
211
212 if (hasAlpha)
213 {
214 for (int x = 0; x < width; x++)
215 {
216 destRow[x * 4 + 0] = srcRow[x * 4 + 2]; // B
217 destRow[x * 4 + 1] = srcRow[x * 4 + 1]; // G
218 destRow[x * 4 + 2] = srcRow[x * 4 + 0]; // R
219 destRow[x * 4 + 3] = srcRow[x * 4 + 3]; // A
220 }
221 }
222 else
223 {
224 for (int x = 0; x < width; x++)
225 {
226 destRow[x * 3 + 0] = srcRow[x * 3 + 2]; // B
227 destRow[x * 3 + 1] = srcRow[x * 3 + 1]; // G
228 destRow[x * 3 + 2] = srcRow[x * 3 + 0]; // R
229 }
230 }
231 }
232 }
233
234 // Unlock and return the Bitmap
235 bitmap.UnlockBits(bmpData);
236 return bitmap;
237 }
238
239 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
240 {
241 if (listView1.SelectedIndices.Count <= 0)
242 return;
243 string[] f = listView1.SelectedItems[0].Text.Split(' ');
244 BioImage b = BioLib.OMERO.GetImage(f[0], datas[selectedIndex].getId());
246 }
247
248 private void searchBox_TextChanged(object sender, EventArgs e)
249 {
250 listView1.Items.Clear();
251 List<ListViewItem> list = new List<ListViewItem>();
252 foreach (ListViewItem item in items)
253 {
254 if(item.Text.Contains(searchBox.Text))
255 list.Add(item);
256 }
257 listView1.Items.AddRange(list.ToArray());
258 }
259 }
260}
System.Drawing.Image Image
Definition ImageView.cs:15
System.Drawing.Graphics Graphics
Definition OMERO.cs:14
BioLib.OMERO OMERO
Definition OMERO.cs:15
static TabsView tabsView
Definition App.cs:10
static string password
Definition Login.cs:15
static string host
Definition Login.cs:15
static int port
Definition Login.cs:16
static string username
Definition Login.cs:15
List< omero.gateway.model.DatasetData > datas
Definition OMERO.cs:21
static System.Drawing.Bitmap RawBytesToBitmap(byte[] rawData, int width, int height, System.Drawing.Imaging.PixelFormat pixelFormat)
Definition OMERO.cs:69
static Icon BytesToIcon(System.Drawing.Bitmap bmp)
Definition OMERO.cs:64
static System.Drawing.Bitmap PixbufToBitmap(Pixbuf pixbuf)
Definition OMERO.cs:178
int selectedIndex
Definition OMERO.cs:23
string Status
Definition OMERO.cs:37
int Progress
Definition OMERO.cs:26
void AddTab(BioImage b)
Definition TabsView.cs:96